Sep 23, 2017Last modified May 10, 2025

Why typeof null is object in javascript

In javascript, the typeof operator is used to find out the data type for a given value.

typeof 10; // "number"
typeof "hello"; // "string"
typeof true; // "boolean"
typeof undefined; // "undefined"
typeof 10; // "number"
typeof "hello"; // "string"
typeof true; // "boolean"
typeof undefined; // "undefined"

When you do typeof null, you get 'Object' as the data type. But why ?

In the initial version of javascript, data types were represented with a type tag along with the actual value. The objects had the type tag 0.

Now, null was intended to represent the absence of an object, similar to a null pointer in other languages. Internally, null was implemented with a value of 0.

The internal "type tag" as it existed in the very early implementations of JavaScript is no longer a directly accessible or explicitly defined structure in the modern JavaScript engine.

Why wasn't this fixed ?

Making this fix in the typeof operator would break a lot of implementations which might have checks around typeof null === object. Hence the decision was made to keep this behavior as is.