14

Possible Duplicate:
Null object in javascript

Hi, I've read this thread about null in JavaScript, but I'm very confused about the identity of null now.

As it is known that typeof(null) evaluates to object because of the language design error, and ECMA states that null is The Null Type.

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

So why people keep saying that null is an object?

Someone said null is a singleton object. Is that how everybody sees the null as in JavaScript too?

Community
  • 1
  • 1
c4il
  • 965
  • 3
  • 16
  • 27
  • 1
    @karim79 Yes, I've read that thread and put my question as a comment to some of the answer, but didn't get an response, so I have raised it here. Thanks – c4il Sep 07 '10 at 21:41
  • 1
    You answered yourself with the quote. null (a value representing itself -- read as: "not anything else" -- and null === null, for all null) is the only occupant of the Null Type. The actual "objectness" is, as you point out, open to argument, but the characteristics of null make it a mostly mute point. –  Sep 07 '10 at 21:44
  • your link "language design error" references a document by Douglas Crockford - I (personally) wouldn't suggest you take everything he says verbatim. – Reinsbrain Nov 18 '14 at 18:19

3 Answers3

11

Null is the absence of an object. Undefined means it hasn't been assigned yet, and null means it has been assigned to be nothing.

Null is not really a singleton object, because dereferencing it will cause an error; for (var x in null) will give you an error. Think back to the pointer days; null was the value a pointer had when it was not pointing to an object.

Anthony Mills
  • 8,676
  • 4
  • 32
  • 51
  • 1
    `for (var x in null);` will not throw on many browsers, although it should, at least as specified on the [3rd edition](http://bclary.com/2004/11/07/#a-12.6.4) of ECMAScript (the `ToObject` operation will throw when the value is `null` or `undefined`). But this has changed, now on [ECMAScript 5](http://www.ecma262-5.com/ELS5_HTML.htm#Section_12.6.4), if the expression is `null` or `undefined`, the statement simply ends (a normal statement completion). – Christian C. Salvadó Sep 07 '10 at 22:14
  • 1
    Nice, ECMA spec on HTML format. I was long looking for that! :) – c4il Sep 07 '10 at 22:26
7

No, null is one of the few primitive types (others being numbers, strings, booleans, and undefined). Everything else is an object, including functions, arrays and regular expressions. Numbers, strings and booleans are often called "object-like", because they have methods, but they are immutable. Objects on the other hand are mutable.

Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443
  • Mutability has nothing to do with being an object. See Object.freeze in EcmaScript 5 which allows creation of immutable objects. Native objects in EcmaScript 3 are also objects and can be immutable. Null is not an object because it can respond to no messages -- has no properties or methods. This is why (null instanceof type) is false for all type. – Mike Samuel Sep 07 '10 at 22:13
  • 1
    @mikesamuel: Removed the "offending" part from my answer :) – Daniel Vassallo Sep 07 '10 at 22:18
  • *Object-like* may be the wrong term to describe how primitives are temporarily boxed when attempted to use as objects. – alex Jan 27 '16 at 10:49
2

null can't be considered an object because it cannot have properties. It is a keyword representing a primitive, like true and false.

> true instanceof Object
false
> null instanceof Object
false
Dagg Nabbit
  • 75,346
  • 19
  • 113
  • 141