From the MDN page about the behaviour of the typeof operator:
Answer from Deepak Ingole on Stack Overflow
null// This stands since the beginning of JavaScript typeof null === 'object';In the first implementation of JavaScript, JavaScript values were represented as a type tag and a value. The type tag for objects was 0.
nullwas represented as the NULL pointer (0x00 in most platforms). Consequently, null had 0 as type tag, hence the "object"typeofreturn value. (reference)A fix was proposed for ECMAScript (via an opt-in), but was rejected. It would have resulted in
typeof null === 'null'.
From the MDN page about the behaviour of the typeof operator:
null// This stands since the beginning of JavaScript typeof null === 'object';In the first implementation of JavaScript, JavaScript values were represented as a type tag and a value. The type tag for objects was 0.
nullwas represented as the NULL pointer (0x00 in most platforms). Consequently, null had 0 as type tag, hence the "object"typeofreturn value. (reference)A fix was proposed for ECMAScript (via an opt-in), but was rejected. It would have resulted in
typeof null === 'null'.
If
nullis a primitive, why doestypeof(null)return"object"?
Because the spec says so.
11.4.3 The
typeofOperatorThe production UnaryExpression :
typeofUnaryExpression is evaluated as follows:
- Let val be the result of evaluating UnaryExpression.
- If Type(val) is Reference, then
a. If IsUnresolvableReference(val) is true, return "undefined".
b. Let val be GetValue(val).- Return a String determined by Type(val) according to Table 20.
Videos
We know that the "typeof" null in javascript is an object, and we also know that it's a bug in JavaScript. I wanted to know what kind of bug it is, and why anyone hasn't resolved it yet.
