This question already exists:
How can I check whether a variable is defined in JavaScript? [duplicate]
Closed 9 years ago.
if (foo) {
// code to run if foo exists
}
results in Uncaught ReferenceError: foo is not defined
So how am i supposed to check if foo exists?
There's several ways - one is
if(typeof foo !== 'undefined'){
// your code here.
}
JS FIDDLE to play with to check
Related
This question already has answers here:
Test for existence of nested JavaScript object key
(64 answers)
Closed 6 years ago.
Is there a short way for accessing a nested object without checking each parent object?
this will throw an exception if foo or bar is undefined:
var exists = (foo.bar.value !== undefined);
I would expect a check function like:
var exists = Object.exists(foo.bar.value);
Is there already something build-in javascript?
Use typeOf
if (typeof myObject!= "undefined") {
console.log('It exists')
}
This question already has answers here:
How to tell if a JavaScript function is defined
(23 answers)
Closed 9 years ago.
I want to know whether a function is exist or is defined before i call it.
I have this line of code:
function renderView(){
// some code
}
You can check the typeof, it returns undefined if it's not defined or out of scope, and function if it's a function
if ( typeof renderView == 'function' ) {
// it exists
}
This question already has answers here:
How does this JavaScript/jQuery syntax work: (function( window, undefined ) { })(window)?
(5 answers)
Next parameter is 'undefined' in jQuery, why? [duplicate]
(3 answers)
Closed 9 years ago.
In John Resig's slideshow on how he was building jQuery 1.4, he mentioned a point where he added an undefined variable to the jQuery closure because "we can re-use (the variable)".
undefined is not an ordinary variable:
> var undefined = 4
undefined
> undefined
undefined
Therefore, we know that undefined is not a variable. So why would an undefined be re-undefined in the jQuery source?
Because in some JavaScript engines it's possible to set undefined to a value. This is to make sure undefined is really undefined.
Additionally to +Rocket Hazmat's answer, you can reduce the file size after compression a bit, when your code uses undefined frequently. That's because a local variable undefined may have its name mangled by the compressor, while the global undefined may not:
foo === undefined;
// ^----- don't touch this, put "undefined" in the compressed result
(function (undefined) {
foo === undefined;
})();
// may however be mangled to
(function(u){foo===u})();
This question already has answers here:
Detecting an undefined object property
(50 answers)
How to check a not-defined variable in JavaScript
(15 answers)
Closed 9 years ago.
var a;
typeof(a);
//undefined
typeof(c);
//undefined
if(c) {}
//throw error
How can I know that c doesn't exist without try catch.
Update after marked duplicate:
typeof initializedVariable and typeof notInitializedVariable both will show 'undefined'. My question is to know whether the variable exists(initialized) or not.
You can use the typeof operator.
if (typeof a === 'undefined') {
// variable is undefined
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
if function does not exist write function - javascript
I have a situation when some function X is being called. After some postbacks this function is no longer declared, but still being called by the code, obviously i get js error saying X is not defined . (call it a bug if you wish) but
It is not under my control to not call it or to change the calling functionality.
What I would like to do is a fail safe that will declare such function if it does not exist. So the logic is:
If function not declared then declare one.
Is that possible in javascript i.e. to declare/register a function dynamically in global scope?
Thanks.
if (typeof window.functionX === 'undefined') {
window.functionX = function() {
// fallback code here
}
}
Sure it is
if(!myFunc) {
myFunc = function() {}
}