Why does typeof NaN return 'number'?

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

    The best way to think of NAN is that its not a known number. Thats why NAN != NAN because each NAN value represents some unique unknown number. NANs are necessary because floating point numbers have a limited range of values. In some cases rounding occurs where the lower bits are lost which leads to what appears to be nonsense like 1.0/11*11 != 1.0. Really large values which are greater are NANs with infinity being a perfect example.

    Given we only have ten fingers any attempt to show values greater than 10 are impossible, which means such values must be NANs because we have lost the true value of this greater than 10 value. The same is true of floating point values, where the value exceeds the limits of what can be held in a float.

    0 讨论(0)
  • 2020-11-22 06:28

    The ECMAScript (JavaScript) standard specifies that Numbers are IEEE 754 floats, which include NaN as a possible value.

    ECMA 262 5e Section 4.3.19: Number value

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

    ECMA 262 5e Section 4.3.23: NaN

    Number value that is a IEEE 754 "Not-a-Number" value.

    IEEE 754 on Wikipedia

    The IEEE Standard for Floating-Point Arithmetic is a technical standard established by the Institute of Electrical and Electronics Engineers and the most widely used standard for floating-point computation [...]

    The standard defines

    • arithmetic formats: sets of binary and decimal floating-point data, which consist of finite numbers (including signed zeros and subnormal numbers), infinities, and special "not a number" values (NaNs)

    [...]

    0 讨论(0)
  • 2020-11-22 06:31

    If using jQuery, I prefer isNumeric over checking the type:

    console.log($.isNumeric(NaN));  // returns false
    console.log($.type(NaN));       // returns number
    

    http://api.jquery.com/jQuery.isNumeric/

    0 讨论(0)
  • 2020-11-22 06:34

    Javascript uses NaN to represent anything it encounters that can't be represented any other way by its specifications. It does not mean it is not a number. It's just the easiest way to describe the encounter. NaN means that it or an object that refers to it could not be represented in any other way by javascript. For all practical purposes, it is 'unknown'. Being 'unknown' it cannot tell you what it is nor even if it is itself. It is not even the object it is assigned to. It can only tell you what it is not, and not-ness or nothingness can only be described mathematically in a programming language. Since mathematics is about numbers, javascript represents nothingness as NaN. That doesn't mean it's not a number. It means we can't read it any other way that makes sense. That's why it can't even equal itself. Because it doesn't.

    0 讨论(0)
  • 2020-11-22 06:35

    NaN is a number from a type point of view, but is not a normal number like 1, 2 or 329131. The name "Not A Number" refers to the fact that the value represented is special and is about the IEEE format spec domain, not javascript language domain.

    0 讨论(0)
  • 2020-11-22 06:36

    The value NaN is really the Number.NaN hence when you ask if it is a number it will say yes. You did the correct thing by using the isNaN() call.

    For information, NaN can also be returned by operations on Numbers that are not defined like divisions by zero or square root of a negative number.

    0 讨论(0)
提交回复
热议问题