Why is typeof null “object”?

前端 未结 8 1444
暗喜
暗喜 2020-11-22 04:51

I\'m reading \'Professional Javascript for Web Developers\' Chapter 4 and it tells me that the five types of primitives are: undefined, null, boolean, number and string.

8条回答
  •  不知归路
    2020-11-22 05:13

    If null is a primitive, why does typeof(null) return "object"?

    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.

    enter image description here

提交回复
热议问题