Why does typeof(null) return "object"?
The curious case of typeof(null)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.
Related posts
- ZeroJS: Exploring the Power of Astro September 21, 2023 Explore Astro framework: blending speed, efficiency, and innovation for modern websites
- What Makes a Great Software Engineer August 5, 2023 Essential qualities that set software engineers apart.
- Server Actions in Next.js: Enhancing User Experience with Optimistic Updates May 6, 2023 Learn how to use Server Actions and experimental useOptimistic hook in Next.js