-1

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?

Community
  • 1
  • 1
PM 77-1
  • 12,933
  • 21
  • 68
  • 111
  • 2
    When you try to access properties on other primitivies, they get transparently wrapped in objects so that they can be linked up to their prototypes, and so there is no error. `null` and `undefined` have no prototypes so this doesn't happen, hence an error. – Paul S. Sep 28 '14 at 16:47
  • 2
    I wouldn't take things from books as chapter and verse. For instance, to say "...key/value pairs...are really objects" is just flat out wrong. I guess he meant to say "key/value pair collections" (aka hashes), but that just goes to show the sloppiness in this and many other books. –  Sep 28 '14 at 16:54
  • @torazaburo - that part is immaterial for my subject. In either case, my Fiddle fully demonstrates the point I was making. – PM 77-1 Sep 28 '14 at 18:03
  • 2
    No, it's not immaterial. I was making the point that authors are sloppy. In this case, he was first sloppy in his reference to "key/value pairs", and then more sloppy in his claim that "Primitive types behave like objects when you use the property access notation", which is what confused you by not clarifying that this did not apply to nulls, and leading you to have to post this question on SO. He might be able to defend himself by claiming a distinction between "primitive values" and "primitive types", but everything I've seen refers to `null` as **both** a primitive value and primitive type. –  Sep 28 '14 at 18:10
  • Not sloppy, focused. My book would suck if I took the time to explain every little detail. You'd have to read too much to extract the value. The fact that primitives get object treatment to handle prototype lookups is itself a bit of a tangent. Digging into the rabbit hole to explain exceptions for something that has no practical use would be a waste of space in a book about building actual applications. YDKJS would be a better place to address all the little edge cases of the JS spec. – Eric Elliott Feb 24 '15 at 19:55

1 Answers1

3

Yes it's an actual primitive.

The exceptions for property access are null and undefined, because they have no wrapper type like strings, booleans and numbers do.

ECMAScript 5, Section 4.3.2 primitive value

member of one of the types Undefined, Null, Boolean, Number, or String as defined in Clause 8.

NOTE A primitive value is a datum that is represented directly at the lowest level of the language implementation.

  • What else `null` and `undefined` lack in comparison to other primitives? – PM 77-1 Sep 28 '14 at 17:04
  • Nothing really. I mean there are some behaviors here and there that will differ, but those are mostly based on the nature of their type. One difference is that if you pass `null` or `undefined` as the first argument to `.call()` or `.apply()`, the `window` object will be used as the `this` value, whereas any other primitive will be represented by its object wrapper. Of course the reason is that they simply don't have an object wrapper. *(This is specific to non-strict-mode code. Strict-mode will use the actual primitive value as `this`.)* –  Sep 28 '14 at 17:12