3

Possible Duplicate:
Javascript null or undefined

null is a reserved word but not a keyword.
Hence it can not be over-written.

undefined is a built in global that can be over-written. This is why you see jQuery re-define it in its IIFE. Just to make sure it was not over-written.

What it the technical distinction of when to use each as specified in ES 5.

I know that I have seen browsers set an un-created localStorage property to either null or undefined depending upon the browser.

localStorage.not_defined === null // sometimes

localStorage.not_defined === undefined // sometimes

How does ES 5 specify their usage in this case and in general?

ES5 provides not clarification:

8.1 The Undefined Type The Undefined type has exactly one value, called undefined. Any variable that has not been assigned a value has the value undefined.

8.2 The Null Type The Null type has exactly one value, called null.

http://www.ecma-international.org/publications/standards/Ecma-262.htm

Community
  • 1
  • 1
  • 1
    @Hiro... javascript spelling clearly isn't your forte. Please, please, **please** start checking the spelling on your tags before picking them. – Charles Sep 26 '12 at 17:56
  • Are you just trying to make sense of it? Or does this affect a real world problem you are having? – Alex Wayne Sep 26 '12 at 18:25
  • Food for thought, how coffee script compiles with some cases around `null` and `undefined`: http://tinyurl.com/9pggy6s – Alex Wayne Sep 26 '12 at 18:33
  • I'm trying to clean up my code so I don't have null in some places and undefined in others...not good practice to use them indiscriminantly...though browsers companies seem to do so. –  Sep 26 '12 at 19:51
  • If undefined is for any variable that has not been assigned a variable...null must be for any variable that does not exist...agree / disagree? I posted the spec below. –  Sep 26 '12 at 19:54
  • @Charles...I think you know it is hopeless. I'll start writing a script for future mistakes. –  Sep 30 '12 at 00:12

2 Answers2

5

The distinction of these two is rather vague and not clarified in the spec.

The common sense is the following: undefined are variables which never have been assigned and non-existing properties.

null however is a state of a variable or property which indicates that it has no value assigned.

Some methods like getElement... explicitely return null to indicate that the resultset is empty. If your function has no return statement, implicitely undefined is returned instead.

In general, always assign null and never undefined.

Christoph
  • 50,121
  • 21
  • 99
  • 128
  • O.K...so I can't remember which Browswer, that does this, but there is one which sets the value to null for a variable that has never been assigned...this is a bug I need to file...I will look into it. –  Sep 27 '12 at 11:37
3

null is a value. The value of nothing is null.

undefined is the lack of a value.

This is how it should be used.

you should never assign undefined to anything. That defeats the purpose. If you want to make your existing property undefined you use the delete keyword.

On the other hand null is a legitimate value to assign to a variable.

jQuery adds an undefined variable to its closure because it is easier to test a === undefined than to write typeof a === 'undefined'.

Sruly
  • 10,200
  • 6
  • 34
  • 39