In JavaScript, we have something called undefined. I said something, because I really don't know if it's a base class, or a built-in variable, or a keyword, or anything else. I just know that it's there.

To see it in action, you can simply write:

typeof undefined;

Can anyone please explain to me why this thing has been inserted into JavaScript? We have null value in this language, thus it shouldn't be something like null. In other languages, when we don't know the value of a property or variable, we simply set it to null. Here we can do the same thing.

How we can use this thing in JavaScript?


There are TWO things you need to understand about undefined...

  • the type undefined that can have only one value.
  • the variable undefined

Now read the following very carefully...

  • There are so many values of type number (10, 10.01, 1e1). But there can be only one value of type undefined, and that value is stored in the variable undefined. That value has NO literal representation -- for example, number values 1, 100, 1e-1 are all literals of type number, but the value stored in the variable undefined has no literal form.

  • undefined is a variable, just a normal variable, that JavaScript declares and assigns it the value of type undefined in the global scope. So you can do all the following...

  typeof undefined;                       // "undefined"

  undefined = 100;
  typeof undefined;                       // "number"

  undefined = void 0;
  typeof undefined;                       // "undefined"

  window.undefined === undefined;         // true
  window.undefined === void 0;            // true
  • if you don't want to use the variable undefined, you can generate the value of type undefined by the expression void 0 -- whose sole purpose is to return a value of type undefined.

...can anyone please explain to me why this thing has been inserted into JavaScript...

NO. Just like no one can explain why undeclared variables go to global scope instead of local. You just need to train yourself to smartly use it instead of trying to find justifications for it's existence.

...we have null value...

Although null can do things undefined does, it is more or less related to objects rather than scalars. Indeed, JavaScript considers null itself an object -- typeof null returns "object".

In my opinion, the bottom line is to NOT try to reason the absolute purposes of undefined and null -- and use them in your code intelligibly, so that your code is readable, maintainable and reusable.

  • 2
    Good explanation @greengit. the bottom line is to NOT try to reason the absolute purposes of undefined and null, seems that JavaScript is a littler far from being a logical language and we simply have to get used to it, like experimenting a new world. – Saeed Neamati Aug 15 '11 at 9:06
  • 1
    "whose sole purpose is to return a value of type undefined". Actually void evaluates an expression, disregards the return value and returns the undefined value. As for the differences. undefined is used by the JS engine to say this thing has no value. null is used by YOU to say this thing has no value. There is a clear distinction. – Raynos Aug 15 '11 at 9:21
  • 1
    undefined = 100; typeof undefined; returns "undefined" for me, if you try to assign to null you get an error. Just wanted to clarify that for future readers. – Josh Rumbut Apr 5 '16 at 16:04

Sorry for answering an older question but the reason you need both undefined and null is simple: in a prototype-based duck-typing language you absolutely must differentiate between "this object does not define a value for X" and "this object says X is nothing/null/empty".

Without this capability there is no way to walk the prototype chain and so inheritance can't work; you must be able to determine that obj.someProp is undefined so you can look at obj.prototype.someProp, and onward up the chain until you find a value. If obj.someProp returned null there would be no way to know if it really was null or just meant "look at my prototype". The only other way around this is to inject some kludgy magic behind the scenes which breaks your ability to fuzz with the prototype chains and do various other bits of JS black magic.

Like much of Javascript, the idea of undefined and null seems wonky and stupid at first, then absolutely brilliant later (then back to wonky and stupid but with a reasonable explanation).

A language like C# will not compile if you access a property that doesn't exist and other dynamic languages often throw exceptions at runtime when you touch a property that doesn't exist, meaning you have to use special constructs to test for them. Plus classes means when an object is instantiated you already know its inheritance chain and all the properties it has - in JS I can modify a prototype 15 steps up the chain and those changes will appear on existing objects.


There is a global variable undefined, which is set to the undefined value. Evil people may set it to something else, so it's probably best not to trust it.

  • The undefined value is the return value of a function that does not return a value.
  • The undefined value is the value of a variable that has not been assigned.
  • The undefined value is the value of a parameter that has not been passed.
  • The undefined value is the value of a property that has not been assigned.
  • 'undefined' is the type of an undeclared variable. (typeof undeclaredVariable == 'undefined'. trying to read the value of an underfined variable will get you a thrown ReferenceError.)

null is subtly broken, because typeof null == 'object'. I don't know why we have undefined and null, probably some historical accident.


Well, undefined is a sentinel value obtained by evaluating something that is not defined as opposed to null, which is the value exactly defined to represent nothing.

var o = { };
> undefined
> Object {}
> undefined
function f() {}
> undefined
function r() { return null; }
> undefined
> undefined
> null

So for example, the return value of a function, that doesn't define a return value is undefined. The value of a property not defined on an object is undefined. The return value of a statement, that is not an expression is undefined (although you will not see this occur other than in the console).
Please note, that you can create a function, that explicitly returns undefined and you can explicitly define a property with the value of undefined on an object, because in the end it is a valid value. You would do so in order to explicitly communicate, that something is not defined.

You could think of the relationship of undefined and null as that between NaN and 0.

  • "The return value of a function, that doesn't define a return value is undefined", do you mean that undefined is void? – Saeed Neamati Aug 15 '11 at 9:01
  • @Saeed: Not quite. void in JavaScript is a unary operator, that takes any argument and returns the value undefined. – back2dos Aug 15 '11 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.