Why is there an global property called undefined?

So, among others these are the types in JavaScript:

  • the Null type which consists of only one value - null
  • the Boolean type which consists of the values true and false
  • the Undefined type which consists of only one value - undefined

Of these four values (null, true, false, undefined), three of them are reserved words (null, true and false), but the last value (undefined) is not a reserved word… It’s an identifier. More precisely, it’s a property of the Global object (which is the window object if we execute inside a browser).

Why is that?

It is a problem, because being a property, you can change its initial value (which is the undefined value of course) to another value like so…

undefined = “hello world”;

If we do that, we risk our code to fail…

if (someObject.property1 !== undefined) {

In the above code, we are comparing the property with the string “hello world” which is not our intention…

If we want to restore the original value of the undefined property, we have to do something like this…

var x;
undefined = x;

If undefined would have been just a keyword like null, true and false, there would not be such issues… Why is it not just a keyword?
What is the benefit of it being a global property?

Šime Vidas

As a property of the global object you can’t actually give it a new value from within a script running in a browser since if you assign a value to undefined in a browser it will assign the value to window.undefined and will not touch the global version.

The window object is not the same as the global object. There are only a few dozen or so properties and methods specific to the global object and to the best of my knowledge none of them can have their values in the global object changed from JavaScript running in a web page since any attempt to do so will just create a new version in the window object.

Examples of things that belong to the global object rather than the window object include:


Changing the definition of any global property would break JavaScript - changing undefined would do relatively little harm compared to redefining Number or Function

I’m just reading the ECMAScript specification here (Edition 3) and it states…

15.1 The Global object.

15.1.1 Value Properties of the Global Object undefined

The initial value of undefined is undefined. This property has the attributes { DontEnum, DontDelete }.

Also, here…

It said that undefined is a property of the global object.

undefined is NOT a property of the window object unless you create such a property. If you do create such a property then it will make the actual field inaccessible in those circumstances where the one you created is referenced. The actual undefined field will still be used in creating variables where a value is not assigned.

If this were not the case then

undefined = 'hello world;
var x;

would set x to ‘hello world’.

I don’t know why undefined is not a reserved word but suspect that there is probably some obscure situation where it makes sense to be able to override it within a particular scope (probably not global scope) and that would not be possible if it were a reserved word.

Never mind…