Why does typeof(null) return "object"?
Fahim's question about `typeof(null)` returning "object" is rooted in JavaScript's specification. According to ECMA-262 5.1, the `typeof` operator for null results in "object". This behavior is not a bug but a deliberate decision made by the language's standards body, which can't be changed due to potential compatibility issues with existing scripts.
generated by granite3.2:8b
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:
- 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.
2nd Explanation
Here is the relevant portion: This is considered a bug, but one which cannot be fixed because it will break too many scripts.