Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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:

undefined;
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?

share|improve this question
1  
    
This was discussed ad nauseum here stackoverflow.com/questions/6999291/… –  Paul Aug 16 '11 at 20:49
add comment

4 Answers

up vote 9 down vote accepted

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.

share|improve this answer
1  
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
    
"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
add comment

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
o
> Object {}
o.foo
> undefined
function f() {}
> undefined
function r() { return null; }
> undefined
f()
> undefined
r()
> 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.

share|improve this answer
    
"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
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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