4

Possible Duplicate:
Why is there a null value in JavaScript?

I don't understand what null is for. 'Undefined' as well.

Community
  • 1
  • 1
David G
  • 94,763
  • 41
  • 167
  • 253
  • 2
    QuestionExists() [http://stackoverflow.com/questions/461966/why-is-there-a-null-value-in-javascript](http://stackoverflow.com/questions/461966/why-is-there-a-null-value-in-javascript) – Wesley Murch Apr 10 '11 at 18:15

5 Answers5

7

It helps to appreciate the difference between a value and a variable.

A variable is a storage location that contains a value.

null means that the storage location does not contain anything (but null itself is just a special kind of value). Read that last sentence carefully. There is a difference between what null is and what null means. 99% of the time you only care about what null means.

Undefined means that the variable (as opposed to the value) does not exist.

Rodrick Chapman
  • 5,437
  • 2
  • 31
  • 32
  • 1
    It's possible to set a variable to `undefined` though, but it's not a good practice. – pimvdb Apr 10 '11 at 19:48
  • 1
    True, I didn't bother to mention what it means for a variable to be defined. In JS a variable is defined when it has a type (contrary to popular opinion, JS is strongly typed... to the degree that the strong/weak dichotomy means anything). Setting a variable to 'undefined' strips it of its type. – Rodrick Chapman Apr 10 '11 at 20:09
  • Please correct me if I'm wrong, but in both cases its type is `undefined`: http://jsfiddle.net/xwxgB/. – pimvdb Apr 10 '11 at 20:51
2

null is an object you can use when creating variables when you don't have a value yet:

var myVal = null;

When you do this, you can also use it to check to see if it's defined:

// null is a falsy value
if(!myVal) {
    myVal = 'some value';
}

I wouldn't use it this way, normally. It's simple enough to use 'undefined', instead:

var myVal; // this is undefined

And it still works as a falsy value.

Creating Objects

When you create an object in javascript, I like to declare all my object properties at the top of my object function:

function myObject() {
    // declare public object properties
    this.myProp = null;
    this.myProp2 = null;

    this.init = function() {
        // ... instantiate all object properties
    };
    this.init();
}

I do this because it's easier to see what the object properties are right off the bat.

typeof
  • 5,812
  • 2
  • 15
  • 19
  • 1
    But why declare it at all if you don't know the value?? – David G Apr 10 '11 at 20:43
  • Well, usually I'd say "because of scope" -- if you declare the variable there, then it is accessible anywhere inside `myObject`. But, it's not that critical in this case. I do it because I want to clearly see my object properties at the top of my definition. – typeof Apr 10 '11 at 21:35
1

If a variable is not known or not initialized in the current scope it is undefined.

If you want to use a variable and indicate that it has an invalid value for instance you may want to assign it null because accessing an undefined variable will throw an error if you try to work with it (despite checking if it is undefined).

If something is undefined you also have the possibility to add it to an object. If a variable is defined but contains null you know that this variable may be used already by another part of your program.

Alex
  • 5,240
  • 1
  • 31
  • 38
0

For example, it can prepare a global variable to be used in more parts of the code.

If you start your code with var iNeedThisEverywhere = null; then you can use it inside more objects/actions in the code, but if you don't do that, then this variable will not be shared throughout the code.

Of course, you will actually put something inside that variable somewhere during the code, but since you defined this variable at the very beginning, it will be shared further anyway.

Frantisek
  • 7,485
  • 15
  • 59
  • 102
0

A variable holding null holds a reference to the "black hole" named null.

10 + 10 + null = null

Null is related historically to databases, http://en.wikipedia.org/wiki/Null_(SQL) (Correct me if I'm wrong here)

A variable not initiated (unknown) is undefined.

regards, /t

Teson
  • 6,644
  • 8
  • 46
  • 69