There are plenty of discussions about what null
in JavaScript actually is. For example, Why is null an object and what's the difference between null and undefined?.
MDN lists null
among primitive values and states that it is:
a special keyword denoting a null value; null is also a primitive value
(The above emphasis is mine)
My last reference will be to Programming JavaScript Applications book by Eric Elliott, which in its Chapter 3. Objects says the following:
In JavaScript, ... even primitive types get the object treatment when you refer to them with the property access notations. They get automatically wrapped with an object so that you can call their prototype methods.
Primitive types behave like objects when you use the property access notations, but you can't assign new properties to them. Primitives get wrapped with an object temporarily, and then that object is immediately thrown away. Any attempt to assign values to properties will seem to succeed, but subsequent attempts to access that new property will fail.
And indeed the following statements will execute without a problem:
"1".value = 1;
(1).value = "1";
false.value = "FALSE";
while his one
null.value = "Cannot set property of null";
throws Uncaught TypeError
. See JS Fiddle.
So at least in this regard, null
behaves differently than other primitives.
Is null
considered a regular primitive in JavaScript?