Mystery and History of typeof null JavaScript

Programming languages all have built-in data structures, but these often differ from one language to another.

In the latest JavaScript (ECMAScript 6) standard defines 7 data types:

  • Six data types that are primitives:
    • Boolean
    • Null
    • Undefined
    • Number
    • String
    • Symbol
  • 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:

ECMA-262-typeof-Operator-Results

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”.

typeof null 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.

It all goes back to the first version of JavaScript when the typeof operator was born. Back then the bits were stored in a different manner than in today’s version of JS and because of that the typeof operator is how it is today.

Axel Rauschmayer has an in-depth explanation regarding this issue, I would suggest you read this article.

As intended

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).

Why isn’t this fixed yet?

There have been discussions between the developers of JS and its community proposing the following change:

Here you can check out the discussion and also the proposals that were taken into account.

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”?

Carol Pelu
Follow me

Carol Pelu

Neutron Star at Neutron Dev
Carol is a self-taught programmer and founder of Neutron Dev. He enjoys the challenge of programming and helping people understand computers. SpaceX and Tesla enthusiast.
Carol Pelu
Follow me

You may also like...