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?
undefined
, or null
as well? - anyone If you 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"
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
If you are interested in knowing whether the variable hasn't been declared or has the value undefined
, then use the typeof
operator, which is guaranteed to return a string:
if (typeof myVar !== 'undefined')
Direct comparisons against undefined
are troublesome as undefined
can be overwritten.
window.undefined = "foo";
"foo" == undefined // true
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
Thanks to @CMS for pointing out that your 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
}
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?
}
Answered 2023-09-20 20:21:49
ReferenceError
if myVariable
is not declared... - anyone undefined
is now described as non-writable, non-configurable and non-enumerable. So, window.undefined = "omg";
will simply fail silently or throw under strict mode. - anyone undefined
is immutable in modern browsers. Setting window.undefined
does nothing. - anyone I personally use
myVar === undefined
Warning: Please note that ===
is used over ==
and that myVar
has been previously declared (not defined).
I do not like typeof myVar === "undefined"
. I think it is long winded and unnecessary. (I can get the same done in less code.)
Now some people will keel over in pain when they read this, screaming: "Wait! WAAITTT!!! undefined
can be redefined!"
Cool. I know this. Then again, most variables in Javascript can be redefined. Should you never use any built-in identifier that can be redefined?
If you follow this rule, good for you: you aren't a hypocrite.
The thing is, in order to do lots of real work in JS, developers need to rely on redefinable identifiers to be what they are. I don't hear people telling me that I shouldn't use setTimeout
because someone can
window.setTimeout = function () {
alert("Got you now!");
};
Bottom line, the "it can be redefined" argument to not use a raw === undefined
is bogus.
(If you are still scared of undefined
being redefined, why are you blindly integrating untested library code into your code base? Or even simpler: a linting tool.)
Also, like the typeof
approach, this technique can "detect" undeclared variables:
if (window.someVar === undefined) {
doSomething();
}
But both these techniques leak in their abstraction. I urge you not to use this or even
if (typeof myVar !== "undefined") {
doSomething();
}
Consider:
var iAmUndefined;
To catch whether or not that variable is declared or not, you may need to resort to the in
operator. (In many cases, you can simply read the code O_o).
if ("myVar" in window) {
doSomething();
}
But wait! There's more! What if some prototype chain magic is happening…? Now even the superior in
operator does not suffice. (Okay, I'm done here about this part except to say that for 99% of the time, === undefined
(and ****cough**** typeof
) works just fine. If you really care, you can read about this subject on its own.)
Answered 2023-09-20 20:21:49
undefined
could be redefined, just because people do use it for such checks. Some people habitually put the constant on the left-hand side when doing such checks: if (undefined == someVariable)
. It only takes a typo for this to silently redefine undefined
: if (undefined = someVariable)
. - anyone undefined
on the LHS. Even if I did, the fact that I use ===
instead of ==
makes the typo extremely unlikely. But the fact that ==
is incorrect is more of a worry. In any case, a bug like that is usually easy to find. Kind of like this bug: typeof x == "undefned"
. - anyone myVar
is indeed undefined the code will throw an error, and it's easy to test - jsfiddle.net/WcM5g The proper way is typeof myVar === 'undefined'
. - anyone var
keyword or a function parameter. I would sell my soul before I (intentionally) wrote code that tried acting on undeclared variables in any which way. Remember, undeclared and undefined are two different concepts in JS. - anyone if (NULL = myVar)
fails to compile and is instantly caught, whereas if (myVar = NULL)
creates a bug that may be difficult to track down depending on what other code is around it. Modern compilers should give you a warning, but many experienced C programmers have already developed the habit of swapping the order. - anyone 2020 Update
One of my reasons for preferring a typeof
check (namely, that undefined
can be redefined) became irrelevant with the mass adoption of ECMAScript 5. The other, that you can use typeof
to check the type of an undeclared variable, was always niche. Therefore, I'd now recommend using a direct comparison in most situations:
myVariable === undefined
Original answer from 2010
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
}
Answered 2023-09-20 20:21:49
'xyz' in window
is a better answer than typeof xyz == "undefined"
because it is testing the wrong thing. The in
operator checks for the existence of a property, regardless of its value, while the question at least appears to be asking how to test if the value of a variable is undefined
. Perhaps a better example for me to choose would have been var foo; "foo" in window
; this returns true while foo
is definitely undefined. - anyone xyz === undefined
better than typeof xyz == "undefined"
? Agreed about globals, but of the two of us only you have been recommending checking properties of window
. - anyone You can use typeof
, like this:
if (typeof something != "undefined") {
// ...
}
Answered 2023-09-20 20:21:49
something !== undefined
, assuming you've already done var undefined
, pre-cautiously. - anyone !==
) is not necessary in this case, since typeof
will always return a string. - anyone typeof
. - anyone typeof
is the safest and less error prone option. More than writing such confusing comment I would have edited the answer to use another term instead of need. Like "You can|should|better|might use typeof
" for example :) - anyone It's been nearly five years since this post was first made, and JavaScript has come a long way. In repeating the tests in the original post, I found no consistent difference between the following test methods:
abc === undefined
abc === void 0
typeof abc == 'undefined'
typeof abc === 'undefined'
Even when I modified the tests to prevent Chrome from optimizing them away, the differences were insignificant. As such, I'd now recommend abc === undefined
for clarity.
Relevant content from chrome://version
:
In Google Chrome, the following was ever so slightly faster than a typeof
test:
if (abc === void 0) {
// Undefined
}
The difference was negligible. However, this code is more concise, and clearer at a glance to someone who knows what void 0
means. Note, however, that abc
must still be declared.
Both typeof
and void
were significantly faster than comparing directly against undefined
. I used the following test format in the Chrome developer console:
var abc;
start = +new Date();
for (var i = 0; i < 10000000; i++) {
if (TEST) {
void 1;
}
}
end = +new Date();
end - start;
The results were as follows:
Test: | abc === undefined abc === void 0 typeof abc == 'undefined'
------+---------------------------------------------------------------------
x10M | 13678 ms 9854 ms 9888 ms
x1 | 1367.8 ns 985.4 ns 988.8 ns
Note that the first row is in milliseconds, while the second row is in nanoseconds. A difference of 3.4 nanoseconds is nothing. The times were pretty consistent in subsequent tests.
Answered 2023-09-20 20:21:49
===
to test for undefined
! - anyone void 0
sounds more unusual to me, 2) you should share your perf tests instead, but mainly 3) your first example (abc === void 0
) throws an exception if abc
is undefined. - anyone typeof
test. - anyone If it is undefined, it will not be equal to a string that contains the characters "undefined", as the string is not undefined.
You can check the type of the variable:
if (typeof(something) != "undefined") ...
Sometimes you 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 you can just evalue the variable. Example:
if (something) {
something(param);
}
Answered 2023-09-20 20:21:49
typeof
is an operator, not a function. - anyone if (typeof foo == 'undefined') {
// Do something
};
Note that strict comparison (!==
) is not necessary in this case, since typeof
will always return a string.
Answered 2023-09-20 20:21:49
};
)? - anyone if(){}
without a ;
... Which minifiers are you referring to? You say that this is how you end every other statement... I guess that's true. But, a block statement {}
is already a statement in and of its own. Adding a ;
makes it two statements, technically. Syntactically, it's redundant. Even automatic semi-colon insertion won't add a semi-colon there... - anyone function() {}
declarations. You’re right though — apparently it’s not required after if
statements, but somehow I still think it makes sense. - anyone Some scenarios illustrating the results of the various answers: http://jsfiddle.net/drzaus/UVjM4/
(Note that the use of var
for in
tests make a difference when in a scoped wrapper)
Code for reference:
(function(undefined) {
var definedButNotInitialized;
definedAndInitialized = 3;
someObject = {
firstProp: "1"
, secondProp: false
// , undefinedProp not defined
}
// var notDefined;
var tests = [
'definedButNotInitialized in window',
'definedAndInitialized in window',
'someObject.firstProp in window',
'someObject.secondProp in window',
'someObject.undefinedProp in window',
'notDefined in window',
'"definedButNotInitialized" in window',
'"definedAndInitialized" in window',
'"someObject.firstProp" in window',
'"someObject.secondProp" in window',
'"someObject.undefinedProp" in window',
'"notDefined" in window',
'typeof definedButNotInitialized == "undefined"',
'typeof definedButNotInitialized === typeof undefined',
'definedButNotInitialized === undefined',
'! definedButNotInitialized',
'!! definedButNotInitialized',
'typeof definedAndInitialized == "undefined"',
'typeof definedAndInitialized === typeof undefined',
'definedAndInitialized === undefined',
'! definedAndInitialized',
'!! definedAndInitialized',
'typeof someObject.firstProp == "undefined"',
'typeof someObject.firstProp === typeof undefined',
'someObject.firstProp === undefined',
'! someObject.firstProp',
'!! someObject.firstProp',
'typeof someObject.secondProp == "undefined"',
'typeof someObject.secondProp === typeof undefined',
'someObject.secondProp === undefined',
'! someObject.secondProp',
'!! someObject.secondProp',
'typeof someObject.undefinedProp == "undefined"',
'typeof someObject.undefinedProp === typeof undefined',
'someObject.undefinedProp === undefined',
'! someObject.undefinedProp',
'!! someObject.undefinedProp',
'typeof notDefined == "undefined"',
'typeof notDefined === typeof undefined',
'notDefined === undefined',
'! notDefined',
'!! notDefined'
];
var output = document.getElementById('results');
var result = '';
for(var t in tests) {
if( !tests.hasOwnProperty(t) ) continue; // bleh
try {
result = eval(tests[t]);
} catch(ex) {
result = 'Exception--' + ex;
}
console.log(tests[t], result);
output.innerHTML += "\n" + tests[t] + ": " + result;
}
})();
And results:
definedButNotInitialized in window: true
definedAndInitialized in window: false
someObject.firstProp in window: false
someObject.secondProp in window: false
someObject.undefinedProp in window: true
notDefined in window: Exception--ReferenceError: notDefined is not defined
"definedButNotInitialized" in window: false
"definedAndInitialized" in window: true
"someObject.firstProp" in window: false
"someObject.secondProp" in window: false
"someObject.undefinedProp" in window: false
"notDefined" in window: false
typeof definedButNotInitialized == "undefined": true
typeof definedButNotInitialized === typeof undefined: true
definedButNotInitialized === undefined: true
! definedButNotInitialized: true
!! definedButNotInitialized: false
typeof definedAndInitialized == "undefined": false
typeof definedAndInitialized === typeof undefined: false
definedAndInitialized === undefined: false
! definedAndInitialized: false
!! definedAndInitialized: true
typeof someObject.firstProp == "undefined": false
typeof someObject.firstProp === typeof undefined: false
someObject.firstProp === undefined: false
! someObject.firstProp: false
!! someObject.firstProp: true
typeof someObject.secondProp == "undefined": false
typeof someObject.secondProp === typeof undefined: false
someObject.secondProp === undefined: false
! someObject.secondProp: true
!! someObject.secondProp: false
typeof someObject.undefinedProp == "undefined": true
typeof someObject.undefinedProp === typeof undefined: true
someObject.undefinedProp === undefined: true
! someObject.undefinedProp: true
!! someObject.undefinedProp: false
typeof notDefined == "undefined": true
typeof notDefined === typeof undefined: true
notDefined === undefined: Exception--ReferenceError: notDefined is not defined
! notDefined: Exception--ReferenceError: notDefined is not defined
!! notDefined: Exception--ReferenceError: notDefined is not defined
Answered 2023-09-20 20:21:49
undefined
within a scope wrapper; this not only protects against the (unusual) case of "oh but undefined
can be redefined`" but also 'helps' with minification. - anyone In this article I read that frameworks like Underscore.js use this function:
function isUndefined(obj){
return obj === void 0;
}
Answered 2023-09-20 20:21:49
Personally, I always use the following:
var x;
if( x === undefined) {
//Do something here
}
else {
//Do something else here
}
The window.undefined property is non-writable in all modern browsers (JavaScript 1.8.5 or later). From Mozilla's documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined, I see this: One reason to use typeof() is that it does not throw an error if the variable has not been defined.
I prefer to have the approach of using
x === undefined
because it fails and blows up in my face rather than silently passing/failing if x has not been declared before. This alerts me that x is not declared. I believe all variables used in JavaScript should be declared.
Answered 2023-09-20 20:21:49
undefined
using scope wrappers: (function($, undefined){ /* undefined is 'abc' in here */ })(jQuery, 'abc');
, which is why ppl complain that it's technically not safe unless you're 100% sure you know where your code is being run. - anyone The most reliable way I know of checking for undefined
is to use void 0
.
This is compatible with newer and older browsers, alike, and cannot be overwritten like window.undefined
can in some cases.
if( myVar === void 0){
//yup it's undefined
}
Answered 2023-09-20 20:21:49
undefined !== void 0
, you likely have other serious problems in said codebase. - anyone myVar
has not been previously declared. - anyone Since none of the other answers helped me, I suggest doing this. It worked for me in Internet Explorer 8:
if (typeof variable_name.value === 'undefined') {
// variable_name is undefined
}
Answered 2023-09-20 20:21:49
// x has not been defined before
if (typeof x === 'undefined') { // Evaluates to true without errors.
// These statements execute.
}
if (x === undefined) { // Throws a ReferenceError
}
Answered 2023-09-20 20:21:49
On the contrary of @Thomas Eding answer:
If I forget to declare myVar
in my code, then I'll get myVar is not defined
.
Let's take a real example:
I've a variable name, but I am not sure if it is declared somewhere or not.
Then @Anurag's answer will help:
var myVariableToCheck = 'myVar';
if (window[myVariableToCheck] === undefined)
console.log("Not declared or declared, but undefined.");
// Or you can check it directly
if (window['myVar'] === undefined)
console.log("Not declared or declared, but undefined.");
Answered 2023-09-20 20:21:49
myVar is not defined
error would be a good thing then, especially when you specifically write "If i forget to declare" [emphasis mine]. I love it when I get errors before my code runs. If you care to see more of my opinion on your answer, I've made relevant comments under my answer. - anyone var x;
if (x === undefined) {
alert ("I am declared, but not defined.")
};
if (typeof y === "undefined") {
alert ("I am not even declared.")
};
/* One more thing to understand: typeof ==='undefined' also checks
for if a variable is declared, but no value is assigned. In other
words, the variable is declared, but not defined. */
// Will repeat above logic of x for typeof === 'undefined'
if (x === undefined) {
alert ("I am declared, but not defined.")
};
/* So typeof === 'undefined' works for both, but x === undefined
only works for a variable which is at least declared. */
/* Say if I try using typeof === undefined (not in quotes) for
a variable which is not even declared, we will get run a
time error. */
if (z === undefined) {
alert ("I am neither declared nor defined.")
};
// I got this error for z ReferenceError: z is not defined
Answered 2023-09-20 20:21:49
I use it as a function parameter and exclude it on function execution that way I get the "real" undefined. Although it does require you to put your code inside a function. I found this while reading the jQuery source.
undefined = 2;
(function (undefined) {
console.log(undefined); // prints out undefined
// and for comparison:
if (undeclaredvar === undefined) console.log("it works!")
})()
Of course you could just use typeof
though. But all my code is usually inside a containing function anyways, so using this method probably saves me a few bytes here and there.
Answered 2023-09-20 20:21:49
var undeclaredvar = window.someUndeclaredVar; if (undeclaredvar === undefined) console.log("it works!").
Please test you example before posting. - anyone