4

Bear with me here, I'm going to try something stupid.

When I evaluate typeof(null) in the console, I get "object", so logically, I should be able to assign properties to it, but null.foo = 42 gives TypeError: Cannot set property 'foo' of null.

Is Javascript just picky when it comes to which global objects are mutable?

Bucket
  • 6,877
  • 9
  • 30
  • 41
  • 3
    possible duplicate of [Why is null an object and what's the difference between null and undefined?](http://stackoverflow.com/questions/801032/why-is-null-an-object-and-whats-the-difference-between-null-and-undefined) – Niet the Dark Absol Dec 09 '13 at 21:05

3 Answers3

10

This page has a nice description of the history here surrounding why typeof(null) gives "object":
JS Data Types - Null

Here is the relevant portion (although I would suggest you read the whole post):

Why does typeof null return "object"?
// What's happening here?
typeof null === "object"; // true

The answer might disappoint some, but the truth is simply because the table above says to do so.

The reasoning behind this is that null, in contrast with undefined, was (and still is) often used where objects appear. In other words, null is often used to signify an empty reference to an object. When Brendan Eich created JavaScript, he followed the same paradigm, and it made sense (arguably) to return "object". In fact, the ECMAScript specification defines null as the primitive value that represents the intentional absence of any object value (ECMA-262, 11.4.11).

To draw a parallel here, consider typeof(NaN) === "number". So why does JavaScript give "number" as the type of NaN (not a number)? It is because NaN is used where numbers appear, it is a value that represents the intentional absence of a number value. Similar reasoning applies to null, the only difference being that null is implemented as a primitive and NaN is actually implemented as a Number object (so NaN.foo = 42 would actually work).

gurkan
  • 894
  • 3
  • 10
  • 21
Andrew Clark
  • 180,281
  • 26
  • 249
  • 286
  • What I get out of this is, `null` is an empty reference, but it's still a reference. Is that a reasonable rationale to draw from the paradigm Eich followed? Similarly, I would say that `undefined` is simply a reference that does not exist. Am I on the right track here? – Bucket Dec 09 '13 at 21:20
  • @DesertIvy You can think of `null` as an empty reference, but I would actually try to avoid the word "reference" because that means different things to different people depending on their favorite language, so it might cause confusion. I like "`null` is a primitive value that represents the absence of an object" better. – Andrew Clark Dec 09 '13 at 21:26
  • That makes sense. Going back to the italicized text in the documentation cited in your answer, it seems that the word `intentional` is extremely relevant to this circumstance. – Bucket Dec 09 '13 at 21:30
3

The specification determines that typeof should return "object" for the null value, and at the same time defines null as a primitive:

A primitive value is a member of one of the following built-in types: Undefined, Null, Boolean, Number, and String; an object is a member of the remaining built-in type Object; and a function is a callable object.

So, null is not an object. That's why you can't assign properties to it. The value returned by typeof is artificial.

bfavaretto
  • 69,385
  • 15
  • 102
  • 145
  • +1 for the explicit statement *"null is not an object"*. From [4.3.11](http://es5.github.io/#x4.3.11) *"primitive value that represents the intentional absence of any object value."* – Blue Skies Dec 09 '13 at 21:22
  • Interesting answer. Is there any inherent advantage in `typeof` returning such a value? `typeof(undefined)` indeed returns `undefined`, so why wouldn't `typeof(null)` similarly return `null`? – Bucket Dec 09 '13 at 21:25
  • @DesertIvy Caught your comment too late... F.J's answer has a good explanation of the rationale behind it, as you probably know by now. – bfavaretto Dec 10 '13 at 00:51
-1

null is a primitive data type. it has one value: null. it does not have a constructor. it is an object that is build in to the language. you can't create an object of the type null, in other words.