Programming languages all have built-in data structures, but these often differ from one language to another.
- Six data types that are primitives:
- and Object
We often use the
typeof operator to return the type string of a given reference according to the table specified in ECMA-262:
As the table above declares, if we go to our browser’s console and type in
typeof null we’ll see that it returns “object”.
Why does type of null returns “object”?
Many developers will say that’s how it is and will give you a link to the table above to prove it.
But I wouldn’t settle for this answer. Just because the Bible says that it doesn’t have to be the right answer, does it?
Let’s put on our explorer hat and dig deeper into this matter to find the answer to this mystery.
Might be a bug
“Wait, what?! Is my language bugged? I knew I shouldn’t have learned JS in the first place!”
No, no, I wouldn’t go that far.
Although many experienced programmers consider this to be a bug, I would rather find out why they think so in the first place.
Axel Rauschmayer has an in-depth explanation regarding this issue, I would suggest you read this article.
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.
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).
Why isn’t this fixed yet?
There have been discussions between the developers of JS and its community proposing the following change:
typeof null === "null"; // true
Whether you consider this to be a bug or not, it won’t go away in the foreseeable future as it will break the existing code that relies exactly on this principle, which means that every web application out there will need to undergo a refactoring.
Did you use typeof null in your code? How would you be affected if typeof null would result in “null” instead of “object”?