Why does typeof NaN return 'number'?

前端 未结 21 1683
误落风尘
误落风尘 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:21

    It means Not a Number. It is not a peculiarity of javascript but common computer science principle.

    From http://en.wikipedia.org/wiki/NaN:

    There are three kinds of operation which return NaN:

    Operations with a NaN as at least one operand

    Indeterminate forms

    • The divisions 0/0, ∞/∞, ∞/−∞, −∞/∞, and −∞/−∞
    • The multiplications 0×∞ and 0×−∞
    • The power 1^∞
    • The additions ∞ + (−∞), (−∞) + ∞ and equivalent subtractions.

    Real operations with complex results:

    • The square root of a negative number
    • The logarithm of a negative number
    • The tangent of an odd multiple of 90 degrees (or π/2 radians)
    • The inverse sine or cosine of a number which is less than −1 or greater than +1.

    All these values may not be the same. A simple test for a NaN is to test value == value is false.

提交回复
热议问题