The jQuery Core Style Guidelines suggest two different ways to check whether a variable is defined.
typeof variable === "undefined"
variable === undefined
object.prop === undefined
Why does jQuery use one approach for global variables and another for locals and properties?
typeof variable === “undefined” in JavaScript. Undefined comes into a picture when any variable is defined already but not has been assigned any value. Undefined is not a keyword. A function can also be undefined when it doesn't have the value returned.
Referencing undeclared variables usually results in a ReferenceError, except when using the typeof keyword. The typeof undefined is the string "undefined" — and undefined is a falsy value that is loosely equal to null but not to other falsy values.
In a JavaScript program, the correct way to check if an object property is undefined is to use the typeof operator. If the value is not defined, typeof returns the 'undefined' string.
A variable that has not been assigned a value is of type undefined . A method or statement also returns undefined if the variable that is being evaluated does not have an assigned value. A function returns undefined if a value was not returned .
For undeclared variables, typeof foo
will return the string literal "undefined"
, whereas the identity check foo === undefined
would trigger the error "foo is not defined".
For local variables (which you know are declared somewhere), no such error would occur, hence the identity check.
I'd stick to using typeof foo === "undefined"
everywhere. That can never go wrong.
I imagine the reason why jQuery recommends the two different methods is that they define their own undefined
variable within the function that jQuery code lives in, so within that function undefined
is safe from tampering from outside. I would also imagine that someone somewhere has benchmarked the two different approaches and discovered that foo === undefined
is faster and therefore decided it's the way to go. [UPDATE: as noted in the comments, the comparison with undefined
is also slightly shorter, which could be a consideration.] However, the gain in practical situations will be utterly insignificant: this check will never, ever be any kind of bottleneck, and what you lose is significant: evaluating a property of a host object for comparison can throw an error whereas a typeof
check never will.
For example, the following is used in IE for parsing XML:
var x = new ActiveXObject("Microsoft.XMLDOM");
To check whether it has a loadXML
method safely:
typeof x.loadXML === "undefined"; // Returns false
On the other hand:
x.loadXML === undefined; // Throws an error
UPDATE
Another advantage of the typeof
check that I forgot to mention was that it also works with undeclared variables, which the foo === undefined
check does not, and in fact throws a ReferenceError
. Thanks to @LinusKleen for reminding me. For example:
typeof someUndeclaredVariable; // "undefined"
someUndeclaredVariable === undefined; // throws a ReferenceError
Bottom line: always use the typeof
check.
Yet another reason for using the typeof-variant: undefined
can be redefined.
undefined = "foo";
var variable = "foo";
if (variable === undefined)
console.log("eh, what?!");
The result of typeof variable
cannot.
Update: note that this is not the case in ES5 there the global undefined
is a non-configurable, non-writable property:
15.1.1 Value Properties of the Global Object
[...]
15.1.1.3 undefined
The value ofundefined
is undefined (see 8.1). This property has the attributes
{ [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.
But it still can be shadowed by a local variable:
(function() {
var undefined = "foo";
var variable = "foo";
if (variable === undefined)
console.log("eh, what?!");
})()
or parameter:
(function(undefined) {
var variable = "foo";
if (variable === undefined)
console.log("eh, what?!");
})("foo")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With