1

If null value of javascript is an empty object so why can't add a property to it? the below code clears my question:

var a = null;

typeof a;
>>> "object"

a.name = 'name';
>>> TypeError: Cannot set property 'name' of null

var a = new Object();

typeof a;
>>> "object"

a.name = 'name';
>>> "name"
Mustafa Shujaie
  • 806
  • 2
  • 10
  • 18
  • `null` is not an "empty object", despite what the `typeof` operator evaluates to. – Phrogz Jan 15 '13 at 20:28
  • "I had to be done in ten days or something worse than JavaScript would have happened." - Brendan Eich – danronmoon Jan 15 '13 at 20:28
  • I think what may be confusing about this is that `typeof null` returns `"object"` although `null` is _not_ actually an object. – dgvid Jan 15 '13 at 20:29
  • I really wish I could close this as a duplicate, but try as I might, I can only find "specific implementation errors" and not a similar general question. In any case, http://stackoverflow.com/questions/461966/why-is-there-a-null-value-in-javascript is an interesting read. –  Jan 15 '13 at 22:43

2 Answers2

8

By definition neither the null value nor the undefined value have any properties, nor can any properties be added to them.

This is summarized nicely for null:

primitive value that represents the intentional absence of any object value.

And likewise, for undefined:

primitive value used when a variable has not been assigned a value.

(null is the only value of the Null-type and undefined is the only value of the Undefined-type.)

Now, for the implementation goodies:

Both of these types represent primitives and the behavior of "primitiveValue.Property" is covered by the internal ToObject method. (See GetValue/PutValue for the start of the rabbit hole.)

From 9.9: ToObject:

The abstract operation ToObject converts its argument to a value of type Object according to ..

  • Undefined => Throw a TypeError exception.
  • Null => Throw a TypeError exception.
  • (and so on)

As far as the comments, see 11.4.3: The typeOf Operator:

Return a String determined by Type(val) according to ..

  • Undefined => "undefined"
  • Null => "object"
  • (and so on)
Ben Aston
  • 53,718
  • 65
  • 205
  • 331
  • 2
    In other words, the question is based on a false premise: neither `null` nor `undefined` are empty objects. – Robert Harvey Jan 15 '13 at 20:28
  • `typeof null === 'object'` is one of the weird JSWTF quirks. It's odd, and it doesn't mean what you think it does. – Alex Wayne Jan 15 '13 at 20:28
  • 1
    It's a quirk that stands since the first implementations of JavaScript. [It *may* be rectified in the next version of ECMAScript](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/typeof#null) by opt-in and will give `typeof null === "null"`. – Mattias Buelens Jan 15 '13 at 20:29
  • @MattiasBuelens Well, it was proposed, but [was also rejected](http://wiki.ecmascript.org/doku.php?id=harmony:typeof_null). – Jonathan Lonowski Jan 15 '13 at 20:31
  • @JonathanLonowski Just great, they don't know what it's supposed to be yet. Updated my comment. :-P – Mattias Buelens Jan 15 '13 at 20:34
  • This is a sentence in the "Professional Javascript for web developers, Nicholas C. Zakas": "Logically, a null value is an empty object pointer, which is why typeof returns “object” when it’s passed a null value". I am really confused about it. null is a primitive type but actually it is a pointer to an empty object! – Mustafa Shujaie Jan 15 '13 at 20:40
  • 2
    @MustafaShujaie The standard's description of the [`null` value](http://es5.github.com/#x4.3.11) may be more useful: "*primitive value that represents the intentional absence of any object value.*" And, it doesn't really explain *why* an "*absence*" is yet an `"object"`, but it does cover the rules that determine the response from the [`typeof` Operator](http://es5.github.com/#x11.4.3). – Jonathan Lonowski Jan 15 '13 at 20:51
  • 1
    @MustafaShujaie As noted in the comments (and I have updated in my answer), `typeof null` returning "object" is only for historical reasons/compatibility at this point. It *would* make much more sense if it returned "null", just as `typeof undefined` evaluates to "undefined". But, as per the specification, it does not. Do not take this to mean that `null` is an object; it is only a primitive value and `null` is most certainly *not* a "pointer". –  Jan 15 '13 at 20:57
1

null is an object in Javascript that represents the absence of an object. You cannot add a property to nothing.

See also: Why is null an object and what's the difference between null and undefined?

Community
  • 1
  • 1
Nivas
  • 18,126
  • 4
  • 62
  • 76