Skip to main content

typeof null

Short answer

javascript
typeof null === "object"

Result: "object"


Why this happens

This is a historical bug in JavaScript, which appeared as far back as the very first version of the language (1995) and stuck around for the sake of compatibility.


Historical reason

In early versions of JS, values were stored as 32-bit tagged data:

  • the first 3 bits indicated the data type
  • the remaining 29 bits held the value itself

Objects used the type tag 000 (three zeros at the start of the 32-bit value).

And for null, the entire value consisted of zeros (0x00).

So when checking typeof null, the engine saw: "ah, the first three bits are 000 → this is an object".

That's how this came to be:

javascript
typeof null // "object"

Why this was not fixed

Because millions of old scripts across the internet may depend on this behavior. Fixing it would cause massive breakage.

So the ECMAScript specification left this as a "known bug", but officially documented it.


How to correctly check for null

Because of this bug, you cannot rely only on typeof to check for null.

Incorrect:

javascript
if (typeof value === 'object') { // could be null! }

Correct:

javascript
if (value === null) { console.log('This is null'); }

or if you want to distinguish null from other objects:

javascript
if (typeof value === 'object' && value !== null) { console.log('This is an object, but not null'); }

Illustrative example

javascript
console.log(typeof null); // "object" console.log(typeof {}); // "object" console.log(typeof []); // "object" console.log(null === {}); // false console.log(null === undefined); // false

All three (null, {}, []) return "object", but these are different data types.


Summary

CheckResultExplanation
typeof null"object"A historical implementation bug
null === nulltrueThe value equals itself
typeof undefined"undefined"Expected behavior
typeof {}"object"A genuine object
typeof []"object"An array is a subtype of object

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.