|
| 1 | +### If null is a primitive, why does typeof(null) return "object"? |
| 2 | + |
| 3 | +##### Explanation-1 |
| 4 | + |
| 5 | +If `null` is a primitive, why does `typeof(null)` return `"object"`? |
| 6 | + |
| 7 | +Because [the spec says so](http://www.ecma-international.org/ecma-262/5.1/#sec-11.4.3). |
| 8 | + |
| 9 | +### [11.4.3][1] The `typeof` Operator |
| 10 | + |
| 11 | +The production _UnaryExpression_ : `typeof` _UnaryExpression_ is evaluated as follows: |
| 12 | + |
| 13 | +1. Let _val_ be the result of evaluating _UnaryExpression_. |
| 14 | +2. If [Type][2](_val_) is [Reference][3], then |
| 15 | + a. If [IsUnresolvableReference][4](_val_) is **true**, return "**`undefined`**". |
| 16 | + b. Let _val_ be [GetValue][5](_val_). |
| 17 | +3. Return a String determined by [Type][6](_val_) according to Table 20. |
| 18 | + |
| 19 | +![enter image description here][7] |
| 20 | + |
| 21 | +[1]: http://www.ecma-international.org/ecma-262/5.1/#sec-11.4.3 |
| 22 | +[2]: http://www.ecma-international.org/ecma-262/5.1/#sec-8 |
| 23 | +[3]: http://www.ecma-international.org/ecma-262/5.1/#sec-8.7 |
| 24 | +[4]: http://www.ecma-international.org/ecma-262/5.1/#sec-8.7 |
| 25 | +[5]: http://www.ecma-international.org/ecma-262/5.1/#sec-8.7.1 |
| 26 | +[6]: http://www.ecma-international.org/ecma-262/5.1/#sec-8 |
| 27 | +[7]: http://i.stack.imgur.com/FzI1R.png |
| 28 | + |
| 29 | +--- |
| 30 | + |
| 31 | +##### Explanation-2 |
| 32 | + |
| 33 | +From [the MDN page about the behaviour of the `typeof` operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof): |
| 34 | + |
| 35 | +<blockquote> |
| 36 | +<h3><code>null</code></h3> |
| 37 | + |
| 38 | +<pre>// This stands since the beginning of JavaScript |
| 39 | +typeof null === 'object'; |
| 40 | +</pre> |
| 41 | + |
| 42 | +<p>In the first implementation of JavaScript, JavaScript values were represented as a type tag and a value. The type tag for objects was 0. <code>null</code> was represented as the NULL pointer (0x00 in most platforms). Consequently, null had 0 as type tag, hence the "object" <code>typeof</code> return value. (<a href="http://www.2ality.com/2013/10/typeof-null.html">reference</a>)</p> |
| 43 | + |
| 44 | +<p>A fix was proposed for ECMAScript (via an opt-in), but <a href="https://web.archive.org/web/20160331031419/http://wiki.ecmascript.org:80/doku.php?id=harmony:typeof_null">was rejected</a>. It would have resulted in <code>typeof null === 'null'</code>.</p> |
| 45 | +</blockquote> |
| 46 | + |
| 47 | +[1]: http://wiki.ecmascript.org/doku.php?id=harmony:typeof_null |
| 48 | + |
| 49 | +--- |
0 commit comments