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
000andnullwas 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, usevalue === null, nottypeof. - To tell a real object apart from
null, you need a compound condition.
Quick example
typeof null === "object"; // trueThe 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:
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:
if (typeof value === 'object') {
// value could easily be null here!
}Right:
if (value === null) {
console.log('This is null');
}Or, when you need to distinguish null from other objects:
if (typeof value === 'object' && value !== null) {
console.log('This is an object, but not null');
}An illustrative example
console.log(typeof null); // "object"
console.log(typeof {}); // "object"
console.log(typeof []); // "object"
console.log(null === {}); // false
console.log(null === undefined); // falseAll three (
null,{},[]) return"object", yet they are different data types.
Summary table
| Check | Result | Explanation |
|---|---|---|
typeof null | "object" | Historical implementation bug |
null === null | true | The 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.nullpasses that check, and the next property access throws aTypeError. - Believing that
nullis an object.nullis its own primitive type; only thetypeofoperator reports"object". - Confusing
nullandundefined.null === undefinedisfalse, althoughnull == undefinedistruebecause 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'.typeofnever returns that string, so the condition is always false.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.