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" 
4
  • null is not an "empty object", despite what the typeof operator evaluates to. Commented Jan 15, 2013 at 20:28
  • "I had to be done in ten days or something worse than JavaScript would have happened." - Brendan Eich Commented Jan 15, 2013 at 20:28
  • I think what may be confusing about this is that typeof null returns "object" although null is not actually an object. Commented Jan 15, 2013 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, stackoverflow.com/questions/461966/… is an interesting read. Commented Jan 15, 2013 at 22:43

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

8 Comments

In other words, the question is based on a false premise: neither null nor undefined are empty objects.
typeof null === 'object' is one of the weird JSWTF quirks. It's odd, and it doesn't mean what you think it does.
It's a quirk that stands since the first implementations of JavaScript. It may be rectified in the next version of ECMAScript by opt-in and will give typeof null === "null".
@MustafaShujaie The standard's description of the null value 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.
@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".
|
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?

1 Comment

Null is not an object, it is a primitive value of type Null.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.