javascript tutorial - [Solved-5 Solutions] How to check for “undefined” in javascript ? - javascript - java script - javascript array



Problem:

What is the most appropriate way to test if a variable is undefined in JavaScript ? I've seen several possible ways:

if (window.myVariable)
Or
if (typeof(myVariable) != "undefined")
Or
if (myVariable) //This throws an error if undefined. Should this be in Try/Catch?

click below button to copy the code. By JavaScript tutorial team

Solution 1:

If we are interested in finding out whether a variable has been declared regardless of its value, then using the in operator is the safest way to go. Consider this example.

// global scope
var theFu; // theFu has been declared, but its value is undefined
typeof theFu; // "undefined"
click below button to copy the code. By JavaScript tutorial team

But this may not be the intended result for some cases, since the variable or property was declared but just not initialized. Use the in operator for a more robust check.

"theFu" in window; // true
"theFoo" in window; // false
click below button to copy the code. By JavaScript tutorial team

If we are interested in knowing whether the variable hasn't been declared or has the value undefined, then use the typeof operator. if (typeof myVar != 'undefined') The typeof operator is guaranteed to return a string. Direct comparisons against undefined are troublesome as undefined can be overwritten.

window.undefined = "omg";
"omg" == undefined // true
click below button to copy the code. By JavaScript tutorial team

As @CMS pointed out, this has been patched in ECMAScript 5th ed., and undefined is non-writable. if (window.myVar) will also include these falsy values, so it's not very robust:

false
0
""
NaN
null
undefined
click below button to copy the code. By JavaScript tutorial team

Thanks to @CMS for pointing out that our third case - if (myVariable) can also throw an error in two cases. The first is when the variable hasn't been defined which throws a ReferenceError.

// abc was never declared.
if (abc) {
    // ReferenceError: abc is not defined
} 
click below button to copy the code. By JavaScript tutorial team

The other case is when the variable has been defined, but has a getter function which throws an error when invoked. For example,

// or it's a property that can throw an error
Object.defineProperty(window, "myVariable", { 
    get: function() { throw new Error("W00t?"); }, 
    set: undefined 
});
if (myVariable) {
    // Error: W00t?
}

click below button to copy the code. By JavaScript tutorial team

Solution 2:

Using typeof is my preference. It will work when the variable has never been declared, unlike any comparison with the == or === operators or type coercion using if. (undefined, unlike null, may also be redefined in ECMAScript 3 environments, making it unreliable for comparison, although nearly all common environments now are compliant with ECMAScript 5 or above).

if (typeof someUndeclaredVariable == "undefined") {
    // Works
}

if (someUndeclaredVariable === undefined) { 
    // Throws an error
}
click below button to copy the code. By JavaScript tutorial team

Solution 3:

We need to use typeof .

if (typeof something != "undefined") {
    // ...
}

click below button to copy the code. By JavaScript tutorial team

Solution 4:

If it is undefined, it will not be equal to a string that contains the characters "undefined", as the string is not undefined. We can check the type of the variable:

if (typeof(something) != "undefined") ...
click below button to copy the code. By JavaScript tutorial team

Sometimes we don't even have to check the type. If the value of the variable can't evaluate to false when it's set (for example if it's a function), then we can just evalue the variable. Example:

if (something) {
  something(param);
}

click below button to copy the code. By JavaScript tutorial team

Solution 5:

if (typeof foo == 'undefined') {
 // Do something
};
click below button to copy the code. By JavaScript tutorial team

Note that strict comparison (!==) is not necessary in this case, since typeof will always return a string.


Related Searches to javascript tutorial - How to check for “undefined” in javascript ?