What's the best way of checking if an object property in JavaScript is undefined?
Sorry, I initially said variable rather than object property. I believe the same == undefined
approach doesn't work there.
What's the best way of checking if an object property in JavaScript is undefined? Sorry, I initially said variable rather than object property. I believe the same |
||||
show 1 more comment |
Use:
|
|||||||||||||||||||||
|
I believe there are a number of incorrect answers to this topic. Contrary to common belief "undefined" is NOT a keyword in javascript, and can in fact have a value assigned to it.
Additionally, The most robust way to perform this test is:
This will always return the correct result, and even handles the situation where myVar is not declared. |
|||||||||||||||||
|
In JavaScript there is null and there is undefined. They have different meanings.
Marijn Haverbeke states, in his free, online book "Eloquent JavaScript" (emphasis mine):
So, I guess the best way to check if something was undefined would be:
Hope this helps! Edit: In response to your edit, object properties should work the same way.
|
|||||||||||||||||||||
|
This worked for me while the others didn't. |
|||||||||||
|
I'm not sure where the origin of using
|
|||||||||||||||||
|
The issue boils down to three cases:
This tells us something I consider important: There is a difference between an undefined member and a defined member with an undefined value.
But unhappily
I'd argue that in some cases it makes more sense (and is clearer) to check whether the property is there, than checking whether it is undefined. For example: I've just been refactoring a bunch of code that had a bunch of checks whether an object had a given property.
Which was clearer when written without a check for undefined.
But as has been mentioned these are not exactly the same (but are more than good enough for my needs). |
|||||||||
|
It's better to use the strict equality operator:
x == undefined also checks whether x is null, while strict equality does not (if that matters).(source) Or you can simply do this:
Here you check if there's any value that can make the variable look false (undefined, null, 0, false, ...). Not a good method for integers ('0' is not false), but might do well for object properties. |
|||||||||||||
|
Crossposting my answer from related question How to check for "undefined" in JavaScript? Specific to this question, see test cases with Some scenarios illustrating the results of the various answers: http://jsfiddle.net/drzaus/UVjM4/ (Note that the use of Code for reference:
And results:
|
|||
|
Returns false if variable is set, and true if is undefined. Then use:
|
|||||||||
|
if you do:
It will fail when the var myvar does not exists because myvar is not defined, so the script is broken and the test has no effect. Because the window object has a global scope (default object) outside a function, a declaration will be 'attached' to the window object. for example:
the global var myvar is the same as window.myvar or window['myvar'] To avoid errors to test when a global variable exists, you better use:
The question if a var really exists doesn't matter, it's value is incorrect. Otherwise, it is silly to initialize variables with undefined, better use the value false to initialize. When you know that all variables that you declare are initialized with false, you can simply check it's type or rely on !window.myvar to check if it has a proper/valid value. So even when the variable is not defined then !window.myvar is the same for myvar=undefined or myvar=false or myvar=0. When you expect a specific type, test the type of the variable. To speed up testing a condition you better do:
When the first and simple condition is true, the interpreter skips next tests. It is always better to use the instance/object of the variable to check if it got a valid value. It is more stable, a better way of programming. Cheers! Kind regards, Erwin Haantjes |
||||
|
The solution is incorrect. In javascript,
will return true because they both are "casted" to a boolean and are false. The correct way would be to check
which is the identity operator... |
|||||||
|
I didn't see (hope I didn't miss it) anyone checking the object before the property. So, this is the shortest and most effective (though not necessarily the most clear):
If the obj or obj.prop is undefined, null, or "falsy", the if statement will not execute the code block. This is usually the desired behavior in most code block statements (in JavaScript). |
|||||
|
You can get array all undefined with path using following code.
jsFiddle link |
|||
|
This doesn't look up through the prototype chain, however. |
|||
|
You can also make it into a function, as shown here:
|
|||||||||
|
This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.
o.prop === undefined
is the way to go, ortypeof(o.prop) == 'undefined'
if there is a risk somebody might define a variable by the nameundefined
. There is a lot of confusion in the answers. Note thato.prop == undefined
will givetrue
ifo.prop
is defined with the valuenull
. – clacke Jul 30 '10 at 8:58typeof(o.prop) == 'undefined'
is still true. To distinguish this case, you need to check'prop' in o
. – Dave Feb 19 '12 at 9:34