Why does typeof(null) return "object"?

Table of Contents

1st Explanation

Why does typeof(null) return “object”?

Even though null is a primitive, typeof(null) still returns “object”. One of the most reasonable explanation:

Because the spec says so.

11.4.3 The typeof Operator

The production UnaryExpression : typeof UnaryExpression is evaluated as follows:

  1. Let val be the result of evaluating UnaryExpression.
  2. If Type(val) is Reference, then a. If IsUnresolvableReference(val) is true, return “undefined”. b. Let val be GetValue(val).
  3. Return a String determined by Type(val) according to Table 20.

2nd Explanation

Null - MDN Web Docs

Here is the relevant portion: This is considered a bug, but one which cannot be fixed because it will break too many scripts.


Updates

Subscribe to my personal newsletter.

Join others! 100% Free. No spam. Privacy Policy

Related posts