DEV Community

Cover image for Is JavaScript Null actually a Object ?
MohitSinghChauhan
MohitSinghChauhan

Posted on • Updated on • Originally published at mohitdotexe.hashnode.dev

Is JavaScript Null actually a Object ?

As a beginner learning JavaScript, you may have come across some odd behaviors that seem confusing at first. One of these is when you check the type of null using the typeof operator, it returns "object" instead of something like "null".

typeof null; // "object"
Enter fullscreen mode Exit fullscreen mode

This doesn't seem to make sense at first. Null is supposed to represent the absence of a value, so why is it considered an object?

To understand why this happens, we need to learn a little history behind the JavaScript language.

A Brief History of JavaScript

JavaScript was created in 1995 by Brendan Eich in just 10 days! The language was initially called Mocha, then LiveScript, and finally JavaScript when it was released alongside Java in Netscape Navigator 2.0.

When creating JavaScript, Eich borrowed ideas from other languages like Self and Scheme. He also had to make sure it integrated well with Java in the browser.

This short timeframe meant that JavaScript had to be designed and implemented very quickly without too much planning ahead. As a result, some imperfections in the language crept in that still affect us today. The typeof null behavior is one such example!

How Values Are Stored in Memory

In the first version of JavaScript, there were only five primitive data types:

  • Undefined
  • Boolean
  • Number
  • String
  • Object

That was it! No other primitive types like null, symbol, or bigint existed.

So at that time, the typeof operator only needed to distinguish between these five types.

To actually understand why null returns "object", we need to learn how values are represented in memory in JavaScript.

In the original implementation of JS, values were stored in memory as a type tag and actual data.

The type tag was used to determine what type of data it was. Objects were assigned the type tag of 0.

Value in Memory:

Type Tag: 0
Data: {...} 
Enter fullscreen mode Exit fullscreen mode

For primitive values like strings, numbers, etc. the type tag would be different:

Type Tag: 1
Data: "Hello World"
Enter fullscreen mode Exit fullscreen mode

When it came to null, which represents the absence of a value, it was decided that it would be stored in memory as a null pointer - which is represented as 0x00 in most languages.

So null ended up being stored in memory as:

Type Tag: 0 
Data: 0x00
Enter fullscreen mode Exit fullscreen mode

Notice that 0 type tag? The same one used for objects!

And this is why when you checked the type of null, it would report "object" - due to it having the same type tag.

Why Not Change It?

So it's clear that this was a bit of an oversight in the initial implementation. Why not just change it so that null has its own type tag and typeof returns "null"?

The main reason is backwards compatibility. When typeof null was changed to "object", a lot of existing code was already relying on this behavior. Changing it would break that code.

Here are some examples:

// Relying on null being an object
function check(val) {
  if (typeof val === 'object') {
    // do something
  }
}

check(null); // would break with a change
Enter fullscreen mode Exit fullscreen mode

Since JavaScript is so widely used, backwards compatibility is crucial. The odd typeof behavior stuck around to avoid potentially breaking the web!

I hope this gives you a better understanding of why JavaScript exhibits this odd behavior with null! The history of programming languages is full of little quirks like this, but they help show how languages are evolving tools.

Connect with me on twitter : MohitSChauhan

Happy Coding ❤️

Top comments (0)