Is JavaScript Null actually a Object ?
Tags: javascript, beginners, webdev, programming
Published: 2023-08-08 17:43:21

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

```js
typeof null; // "object"
```

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.
<br />
## 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!
<br />
## 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: {...} 
```

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

```
Type Tag: 1
Data: "Hello World"
```

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
```

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.
<br />
## 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:

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

check(null); // would break with a change
```

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](https://twitter.com/MohitSChauhan)

Happy Coding ❤️