Skip to main content

Why typeof null is object

typeof null returns "object", and that is a historical JavaScript bug rather than a logical property of the language. It has been there since 1995 and is kept deliberately so that millions of old scripts do not break.

Theory

TL;DR

  • typeof null === "object", that is the result and nobody is going to change it.
  • The cause is the early 32-bit value representation where the object tag was 000 and null was all zeros.
  • It is an officially acknowledged "known bug", documented in ECMAScript.
  • It was never fixed for backward compatibility: a change would break an enormous amount of working code.
  • To check for null, use value === null, not typeof.
  • To tell a real object apart from null, you need a compound condition.

Quick example

javascript
typeof null === "object"; // true

The result: "object".

The historical reason

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

  • the first 3 bits encoded the data type;
  • the remaining 29 bits held the value itself.

Objects used the type tag 000, that is three zeros at the start of the 32-bit value. And null was a value made entirely of zeros (0x00).

So when evaluating typeof null, the engine saw the first three bits as 000 and concluded it was an object. That is how this came about:

javascript
typeof null; // "object"

Why it was never fixed

Because millions of old scripts all over the internet may depend on this behaviour. Fixing it would cause mass breakage.

So the ECMAScript specification kept it as a "known bug" but documented it officially.

How to check for null correctly

Because of this bug you cannot use typeof alone to check for null.

Wrong:

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

Right:

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

Or, when you need to distinguish null from other objects:

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

An 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", yet they are different data types.

Summary table

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

Common mistakes

  • Detecting an object with typeof value === 'object' and no extra condition. null passes that check, and the next property access throws a TypeError.
  • Believing that null is an object. null is its own primitive type; only the typeof operator reports "object".
  • Confusing null and undefined. null === undefined is false, although null == undefined is true because of type coercion.
  • Hoping the bug will eventually be fixed. It is frozen into the specification forever, so write code that accounts for it.
  • Using typeof value === 'null'. typeof never returns that string, so the condition is always false.

Short Answer

Interview ready
Premium

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