Why does typeof NaN return 'number'?

前端 未结 21 1239
误落风尘
误落风尘 2020-11-22 05:38

Just out of curiosity.

It doesn\'t seem very logical that typeof NaN is number. Just like NaN === NaN or NaN == NaN returning

21条回答
  •  鱼传尺愫
    2020-11-22 06:20

    typeof NaN returns 'number' because:

    • ECMAScript spec says the Number type includes NaN:

      4.3.20 Number type

      set of all possible Number values including the special “Not-a-Number” (NaN) values, positive infinity, and negative infinity

    • So typeof returns accordingly:

      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
        1. If IsUnresolvableReference(val) is true, return "undefined".
        2. Let val be GetValue(val).
      3. Return a String determined by Type(val) according to Table 20.

                      Table 20 — typeof Operator Results
      ==================================================================
      |        Type of val         |              Result               |
      ==================================================================
      | Undefined                  | "undefined"                       |
      |----------------------------------------------------------------|
      | Null                       | "object"                          |
      |----------------------------------------------------------------|
      | Boolean                    | "boolean"                         |
      |----------------------------------------------------------------|
      | Number                     | "number"                          |
      |----------------------------------------------------------------|
      | String                     | "string"                          |
      |----------------------------------------------------------------|
      | Object (native and does    | "object"                          |
      | not implement [[Call]])    |                                   |
      |----------------------------------------------------------------|
      | Object (native or host and | "function"                        |
      | does implement [[Call]])   |                                   |
      |----------------------------------------------------------------|
      | Object (host and does not  | Implementation-defined except may |
      | implement [[Call]])        | not be "undefined", "boolean",    |
      |                            | "number", or "string".            |
      ------------------------------------------------------------------
      

    This behavior is in accordance with IEEE Standard for Floating-Point Arithmetic (IEEE 754):

    4.3.19 Number value

    primitive value corresponding to a double-precision 64-bit binary format IEEE 754 value

    4.3.23 NaN

    number value that is a IEEE 754 “Not-a-Number” value

    8.5 The Number Type

    The Number type has exactly 18437736874454810627 (that is, 253−264+3) values, representing the double-precision 64-bit format IEEE 754 values as specified in the IEEE Standard for Binary Floating-Point Arithmetic, except that the 9007199254740990 (that is, 253−2) distinct “Not-a-Number” values of the IEEE Standard are represented in ECMAScript as a single special NaN value. (Note that the NaN value is produced by the program expression NaN.)

提交回复
热议问题