How can I check for "undefined" in JavaScript? [duplicate]
Asked Answered
T

16

3053

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?
Tallu answered 2/8, 2010 at 17:53 Comment(11)
Do you want to check for only undefined, or null as well?Episode
check this stackoverflow.com/questions/27509/…Evelyne
@Robert - that question has an accepted answer that answers here have proven to be wrongRoadside
See: How to check for undefined in javascript?, and whether a variable is undefined and How to handle ‘undefined’ in javascriptEgghead
Explanation for defined/undefined object properties see this: [https://mcmap.net/q/21057/-detecting-an-undefined-object-property][1] [1]: https://mcmap.net/q/21057/-detecting-an-undefined-object-propertySelfsuggestion
That "duplicate" is about object properties, so some of the answers don't apply very well to this question, asking about variables.Tisiphone
know about undefined and it's relation with scope codepen.io/grumpy/post/undefined-scope-in-javascriptDownhaul
myVariable === undefinedMacomber
The question is good. This post shows how bad js is specially that it is a primary web development tool. It shows how fragile the web development is in this day and age!!!Ylla
What about myVar ===void(0)?Jocasta
Reminder: typeof returns string, therefore there is no need to use !== or === with an explicit string operand different to empty string, such as "undefined"; != or == suffices.Bradski
H
3200

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?
}
Humorous answered 2/8, 2010 at 17:58 Comment(27)
@Anurag, the third case will throw a ReferenceError if myVariable is not declared...Tso
@CMS - thanks, I somehow imagined myVariable was declared somewhere, just without a value.Humorous
@Anurag, you're welcome, since you talk about ES5, maybe is worth mentioning that 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.Tso
I abhor your argument against using undefined. (Also, you want triple equal signs for comparision against it.)Polyptych
Can "typeof" be redefined?Tallu
typeof is a language statement, it cannot be redefined any more than if/else/while/for/function etc. could be.Whiffet
@Whiffet If thats the case, then I'll infer that language statements in general can't be redefined ;)Tallu
Would anyone know the minification difference between npup's approach and trinthis's approach of myVar === undefinedTallu
If you're worried about minification, you can use a variable instead of a string literal to compare against an object's type: var UNDEF = "undefined"; if (typeof someVar == UNDEF)...Stretto
@MakerOfThings7 - My experience is that you can see improvements by declaring your own version of certain variables. Minifyers typically won't touch this either, so if it is referenced enough times, it is worth to do var self = this; in the scope and the minifyer will shorten it to a or something. Same thing with undefined - if you use it a couple of times in the scope, you can get the "wasted" chars (and more) back via the minifyer. And as for undefined, you are then also sure to get a really undefined undefined, which I think is great.Vaccaro
Anurag - Thanks for the update... you have the best answer and I'd +1 you again if possible..Tallu
is it okay to check truth with if(window.myVar) ?Madeira
undefined is immutable in modern browsers. Setting window.undefined does nothing.Kolyma
This is the best answer on whole internet except it lacks few things: 1) you could also use obj.hasOwnProperty('who'); in place of 'who' in obj. 2) even in browsers where undefined can be redefined doing this will return true: `suppose undefined = omg. ok... so var x; typeof x === undefined;//true typeof x == undefined;//false' i am little unsure about second please confirm.Nelsonnema
Since yours is the accepted answer, I'll ask you what do you think of myVar == void 0 posted here: https://mcmap.net/q/21019/-how-can-i-check-for-quot-undefined-quot-in-javascript-duplicateTallu
@MuhammadUmer—the in test only works with the global object, it doesn't work for any other execution context so not a general solution (or even useful most of the time).Hesitancy
Can I ask one thing, why someone want to define a getter that throws an exception every time? Am I missing something? Is it a corner case that someone want to abuse the defineProperty?Abad
Can I compare with undefined instead of using typeof()=='undefined'?Fluoridation
@andig: Yes. Never use typeof to check for undefined.Baughman
According to this jsPerf: jsperf.com/in-vs-typeof/4 the typeof operator supersedes the in operator in performance by far (~99% slower in Chrome)Grateful
typeof myVar is really useful.Daemon
While undefined is not writeable in ES5, nothing prevents code from redefining undefined in a local variable or argument. So use myVar === undefined if you know no code above yours in the same function will every declare an undefined. Otherwise use typeof myVar === "undefined".Intranuclear
In modern browsers, you can override undefined only inside a scope that is not global.Edelstein
@CMS your use case doesn't apply in a function. ``` function test() { // abc was never declared. if (abc) { // ReferenceError: abc is not defined } } test() ``` will not throwEffective
@Humorous might be worth including this also both an undefined and null variables are false when using !! operator, e.g. (!!x) would be falseFinial
function(x){// how here to identify if x is set or not?}Universalize
What about theFu ===void(0)?Jocasta
P
1548

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.)

Polyptych answered 2/8, 2010 at 18:26 Comment(25)
It's marginally more likely that 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).Stretto
I never write code that has 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".Polyptych
== is not necessarily incorrect: it would be correct if the intention was to match null and undefined. However, I do agree that writing code like that is a pretty terrible idea.Stretto
How could this be upvoted 41 times, it simply doesn't work. If 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'.Intonation
@Laurent: A joke right? This assumes the variable was declared in some way or the other, such as by a 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.Polyptych
@ThomasEding, some libraries define namespaces by first checking if the variable exists and creating it if it doesn't (see for instance blog.arc90.com/2008/06/06/…), and doing something like if (ns === undefined) ns = {}; would not work. If the variable is already declared then indeed no problem.Intonation
@Laurent: Again, that is not the only way to do it: if (window.ns === undefined) window.ns = {}; But I prefer: if (!("ns" in window)) window.ns = {}; Though admittedly I normally write this kind of code not for namespaces, but for methods: if (!Array.prototype.map) Array.prototype.map = ... (Also a 5 year old JS article is hardly state of the art, though I'm sure there isn't short supply of that exact code sequence.)Polyptych
@ThomasEding, I think we're in agreement actually. My initial reaction was just that the code above, on its own (i.e. with myVar not being declared anywhere previously), would not work. However, if the variable is indeed already declared, and the programmer is really sure that it is declared, then your code is absolutely fine. I'm not too worried about "undefined" being set to a different value as any such environment would probably be broken beyond salvation anyway.Intonation
i clarified your answer based on the back-and-forth with @Laurent so that "casual readers" won't be confused.Nissen
If you're worried about undefined being redefined just do myVar === void 0Scan
very bad advice. some library could define a global 'undefined' and kill your code. use the 'typeof' method, it's safe.Aweigh
@ThomasEding Your answer and comments are very practical and useful to the discussion. One thing though - beware of 'i never write...', because your librarys and team members may not be as disciplined!Triadelphous
@TimDown if someone accidentally reassigns undefined, wouldn't you want the program to bug out so that you'd know there was an error and eventually be able to track it down?Stylography
@Andy: Definitely, which is one reason not to use undefined on the left hand side of a comparison.Stretto
@TimDown yeah, I certainly hope no one has a strange habit of putting null or undefined on the lhs, in any programming language!Stylography
@Stylography In C (and C++), it is both common and good practice to reverse operands like that, to avoid typos. 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.Magnien
@GrandOpener: -Wall -Werror. QED. Now the code doesn't have to read like something that came out of Voynich manuscript.Polyptych
@ThomasEding - Boost and other libraries have known warnings; boost.org/doc/libs/1_61_0/more/getting_started/….Integrate
Upvoted for both: elegance and pragmatism. If you really, really, really cannot get a wink of sleep, knowing that undefined might not be what it should be, wrap your code with an IIFE like so: (function(undefined) { if (isItOrNot === undefined) doSomething(); })();.Eliaeliades
This will only work, if variable was declared before. If variable was not declared before, this throws an warning: '<variable> is not defined at ...' To be save, use >> typeof <variable> !== "undefined" << Watch out, undefined is a String here, so encapsulate it in quotes!Anywise
What is wrong with getting an error on a undeclared variable? In most comileable programming languages you cannot even compile when using a variable without declaring it. So I would assume using this can eliminate a lot of mistakes. If you still want to use some JS-magic, use typeof with comments, why it could be possible, the variable is not even declared.Hydrobomb
Actually the undefined without quotes worked for meMacomber
its not working for me in chrome , uncaught refrence error when you never defined myvar but typedef myvar worksInsnare
What about myVar ===void(0)?Jocasta
Trying myVar === undefined and got exception- Uncaught ReferenceError: myVar is not defined at <anonymous>:1:1Gris
S
375

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
}
Stretto answered 2/8, 2010 at 18:5 Comment(9)
You might want to check if a particular global variable representing a piece of functionality has already been defined. For example, library code may wish to check that the library has not already previously been included.Stretto
'xyz' in window or 'xyz' in self are much betterWilkerson
@JamiePate: What about window.foo = undefined? "foo" in window will return true.Stretto
don't do that, use null instead. if you have to you can 'xyz' in window && window.xyz !== undefinedWilkerson
@JamiePate: Just to be clear, I disagree that '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.Stretto
It really depends on the goal. 'checking for undefined' should almost always be xyz === undefined imo. If you want to check if a variable exists in an object 'xyz' in abc is the way to go. implicit use of globals (aka members of the window object) is not a good way to go. Avoid relying on globals in the first place if at all possible.Wilkerson
@JamiePate: Why is 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.Stretto
It's redundant in most cases (and less readable). If you know xyz is a declared variable, why go through the extra trouble? Type checking and string comparison are much slower in some browsers, so if you do it a lot in a tight loop you will lose some performance. jsperf.com/type-of-undefined-vs-undefined/6Wilkerson
@JamiePate: Fair enough. There's the old argument against xyz === undefined about undefined being mutable, but that's gone in ECMAScript 5 and I never found it a particularly persuasive argument anyway.Stretto
M
127

You can use typeof, like this:

if (typeof something != "undefined") {
    // ...
}
Mosque answered 6/6, 2010 at 20:22 Comment(7)
Or just something !== undefined, assuming you've already done var undefined, pre-cautiously.Cordelier
Good to see you added the quotes now. However, as mentioned in my answer, note that strict comparison (!==) is not necessary in this case, since typeof will always return a string.Electromechanical
Mathias: using strict or non-strict comparison here is a matter of personal taste. Both will always work, and neither is more correct. It could depend on whether your default position is to always use strict comparison unless specifically requiring type coercion (as recommended by Crockford, for example) or whether you prefer to use non-strict comparison except when strictness is required.Stretto
This is inaccurate. You absolutely don't need to use typeof.Polyptych
@ThomasEding you sure? What if you check on a variable that could contain a string with potentially a (valid) value of "undefined"? (e.g. var adjective = "undefined";)Detainer
@Detainer Look at the many other answers to see that there are alternatives. Hence you don't need to use typeof.Polyptych
Oh, now I got what you mean; your comment is misleading because was looking like related to the correctness of the code. Yes, one doesn't need to use it, since everything is matter of tastes; if you know what you are doing you don't even need to sanitize user inputs; that doesn't mean that it shouldn't be done. In this case, among all the answers, using 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 :)Detainer
S
76

Update 2018-07-25

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:

  • Google Chrome: 67.0.3396.99 (Official Build) (64-bit) (cohort: Stable)
  • Revision: a337fbf3c2ab8ebc6b64b0bfdce73a20e2e2252b-refs/branch-heads/3396@{#790}
  • OS: Windows
  • JavaScript: V8 6.7.288.46
  • User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36

Original post 2013-11-01

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.

Suit answered 1/11, 2013 at 4:52 Comment(5)
Aww, so heartbreaking that this is -1; I spent a good amount of time testing this. Oh well. It's good info, so I'll leave it here. Remember, don't use === to test for undefined!Suit
i assume the -1 was because of 1) <q>and clearer at a glance to someone who knows what void 0 means</q>, since 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.Nissen
added your method to my test list and it does check out (not that I doubted you) -- jsfiddle.net/drzaus/UVjM4/8Nissen
I think the best compromise between clarity and speed, given these numbers (which are from a while ago), is the typeof test.Suit
I find it amazing that the undefined compare is slower than to void 0. I imagine that the running JS version is new enough for undefined to be guaranteed constant. So sad.Polyptych
Z
28

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);
}
Zanze answered 6/6, 2010 at 20:23 Comment(4)
No need for the parentheses: typeof is an operator, not a function.Stretto
@Tim - It can be used both ways.Episode
@Tim: @Nick is correct. See developer.mozilla.org/en/Core_Javascript_1.5_Reference/…Electromechanical
Yes, I know that it works with the parentheses, which is because the parentheses here form the grouping operator that simply evaluates and returns the operand inside. I merely said that they were unnecessary.Stretto
E
26
if (typeof foo == 'undefined') {
 // Do something
};

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

Electromechanical answered 6/6, 2010 at 20:26 Comment(6)
What's with the semi-colon (};)?Cordelier
@J-P: The semicolon after the closing brace is just an empty statement.Obliquely
@Gumbo, sorry, what I meant to ask was: "What purpose is the semi-colon serving?"Cordelier
@J-P That’s just a personal preference. I like to add optional semicolons — the if block can be seen/rewritten as one line of code, and then it makes sense to append the semicolon, because that’s how I end pretty much every other statement. if (typeof foo == 'undefined') { }; Also, this ensures compatibility with some JavaScript minifiers. I’m aware JSLint advises against this, but I just don’t see the point — these semicolons are harmless and if anything, enforce a slightly stricter coding style.Electromechanical
I've not encountered a minifier that can't handle 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...Cordelier
@J-P: I guess I started doing it years ago after reading the Packer documentation. Packer expects semicolons after function() {} declarations. You’re right though — apparently it’s not required after if statements, but somehow I still think it makes sense.Electromechanical
N
19

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
Nissen answered 13/1, 2013 at 15:39 Comment(1)
note the use of 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.Nissen
G
18

In this article I read that frameworks like Underscore.js use this function:

function isUndefined(obj){
    return obj === void 0;
}
Goldcrest answered 19/12, 2013 at 10:42 Comment(0)
D
15

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.

Daudet answered 20/12, 2013 at 10:24 Comment(3)
you can redeclare 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.Nissen
Great point about wanting undeclared variable to blow up - this does not happen with typeof.Damar
With the July 2021 Chrome for Windows (Version 92.0.4515.107), I tried: if ( myVar === undefined ), if ( myVar === 'undefined' ), if ( myVar === void 0), or if ( !myVar ) All failed! Every case threw an undefined JavaScript error, and effectively returned a 'true' causing the branch to be taken... Solution: if ( !window.myVar ) myVar = false; That's all I needed, get it declared globally as false, if a previously library wasn't included to initialize it to 0/false. So FYI, the best solution will involve the use of the window object!Seroka
T
12

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
}
Tifanytiff answered 19/2, 2014 at 13:20 Comment(3)
This is underrated and IMHO a preferable way to check for something being undefined.Refutative
Absolutely correct, but I imagine if undefined !== void 0, you likely have other serious problems in said codebase.Polyptych
And it exposes to throwing error when myVar has not been previously declared.Detainer
P
8

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
}
Portage answered 22/11, 2012 at 16:47 Comment(0)
S
6
// x has not been defined before
if (typeof x === 'undefined') { // Evaluates to true without errors.
   // These statements execute.
}

if (x === undefined) { // Throws a ReferenceError

}
Sussex answered 8/8, 2014 at 7:9 Comment(0)
P
5

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.");
Pigeonwing answered 13/4, 2013 at 4:49 Comment(1)
Getting such a 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.Polyptych
F
4
    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 
Folger answered 4/3, 2014 at 18:37 Comment(0)
I
1

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.

Introspect answered 2/8, 2010 at 18:2 Comment(1)
It will give ReferenceError if the var undeclaredvar is really undeclared. It this is attribute - then it works, example: var undeclaredvar = window.someUndeclaredVar; if (undeclaredvar === undefined) console.log("it works!"). Please test you example before posting.Kneeland

© 2022 - 2024 — McMap. All rights reserved.