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"
By definition neither the
nullvalue nor theundefinedvalue have any properties, nor can any properties be added to them.This is summarized nicely for null:
And likewise, for undefined:
(
nullis the only value of the Null-type andundefinedis 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:
As far as the comments, see 11.4.3: The typeOf Operator: