Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding “undefined” in Javascript: how it works, how to safely check against it and whether reassignment is possible

I’ve been reading about undefined in JavaScript and now I am not sure if my understanding is correct. There is a lot of talk around how to check for undefined but somehow I couldn’t find any mentioning of something that to me seems fundamental to understanding of how undefined actually works (undefined being property on host object). This is the reason for this question, I need to confirm that what I understand is correct and if I’m wrong I would appreciate clarification.

Okay, first of all, undefined is property on host object (window in browsers) so it’s perfectly legal to use:

window.undefined

The value of this property is type "undefined". This is one of the JavaScript types along with Object, String, Number and Null. So if I do:

if(someVar===undefined) {}

I’m actually checking against window.undefined property, whatever it contains, is that right?

So this code below would be pretty dumb as this would check someVar only against the string "undefined", not the type nor the property of the window object, right?

if(someVar==='undefined') {}

This below would be also incorrect as this would check against the window.undefined property (whatever it contains):

if(typeof someVar===undefined) {}

So, to sum it up, the only proper and cross-browser way to check for undefined is to use typeof e.g.:

if(typeof someVar==='undefined')

Is that right?

Also in ES5 window.undefined cannot be reassigned but it’s perfectly legal in older browsers right?

This however can still be done and is evil if my understanding is right:

(function() { 
  var undefined=66;
  alert(undefined);
})()

I would appreciate clarification if I misunderstood how undefined works in JavaScript.

like image 379
spirytus Avatar asked Dec 17 '13 01:12

spirytus


People also ask

How does JavaScript undefined work?

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 .

How do you check if JavaScript object is undefined?

Use the strict equality (===) operator to check if an object property is undefined, e.g. if (obj. age === undefined) {} . The strict equality operator will return true if the property is undefined and false otherwise.

Can you assign undefined in JavaScript?

YES, you can, because undefined is defined as undefined.


2 Answers

You're almost correct. Except for this:

The value of [window.undefined] is type "undefined". This is one of Javascriupt types along with Object, String, Number, and Null

There are 3 undefined in javascript. The global variable undefined, the value undefined and the type undefined.

Even if the global variable undefined is overridden, the value undefined still exists. There are several ways to get it one of which is an empty argument to a function, another is a variable declaration without assigning anything:

// Note: Probably need older browsers to assign to undefined:
window.undefined = 1;

(function(foo){ // the value of foo is undefined;
    var bar;    // the value of bar is undefined;

    return [foo === bar, foo === window.undefined]; // returns [true,false]
})();

Note carefully that in the example above we're checking the value, not the type. Yes === checks type and value but if you replace === with == the result would be the same.

The value undefined has type undefined ('Undefined' in the spec and documentation but typeof returns 'undefined') and type undefined is only valid for the value undefined.

like image 177
slebetman Avatar answered Sep 20 '22 01:09

slebetman


That's all fine, plus:

  • you can use void 0 to reliably "generate" the real undefined value (or not-a-value; it's kind-of zen)
  • in a function, you can reference an argument that you know isn't supplied to get a reliable undefined

    (function( undefined ) {
      // ...
    })();
    

    This second example is not really the clearest code in the world, but you'll see it sometimes in common public codebases, tutorials, etc.

like image 32
2 revs Avatar answered Sep 22 '22 01:09

2 revs