Why does typeof NaN return 'number'?

Just out of curiosity.

It doesn't seem very logical that typeof NaN is number. Just like NaN === NaN or NaN == NaN returning false, by the way. Is this one of the peculiarities of javascript, or would there be a reason for this?

Edit: thanks for your answers. It's not an easy thing to get ones head around though. Reading answers and the wiki I understood more, but still, a sentence like

A comparison with a NaN always returns an unordered result even when comparing with itself. The comparison predicates are either signaling or non-signaling, the signaling versions signal an invalid exception for such comparisons. The equality and inequality predicates are non-signaling so x = x returning false can be used to test if x is a quiet NaN.

just keeps my head spinning. If someone can translate this in human (as opposed to, say, mathematician) readable language, I would be gratefull.


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.


    Well, NaN is still a numeric type, despite the fact it actually stands for Not-A-Number :-)

    NaN just means the specific value cannot be represented within the limitations of the numeric type (although that could be said for all numbers that have to be rounded to fit, but NaN is a special case).

    A specific NaN is not considered equal to another NaN because they may be different values. However, NaN is still a number type, just like 2718 or 31415.


    As to your updated question to explain in layman's terms:

    A comparison with a NaN always returns an unordered result even when comparing with itself. The comparison predicates are either signalling or non-signalling, the signalling versions signal an invalid exception for such comparisons. The equality and inequality predicates are non-signalling so x = x returning false can be used to test if x is a quiet NaN.

    All this means is (broken down into parts):

    A comparison with a NaN always returns an unordered result even when comparing with itself.

    Basically, a NaN is not equal to any other number, including another NaN , and even including itself.

    The comparison predicates are either signalling or non-signalling, the signalling versions signal an invalid exception for such comparisons.

    Attempting to do comparison (less than, greater than, and so on) operations between a NaN and another number can either result in an exception being thrown (signalling) or just getting false as the result (non-signalling or quiet).

    The equality and inequality predicates are non-signalling so x = x returning false can be used to test if x is a quiet NaN.

    Tests for equality (equal to, not equal to) are never signalling so using them will not cause an exception. If you have a regular number x , then x == x will always be true. If x is a NaN , then x == x will always be false. It's giving you a way to detect NaN easily (quietly).


    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)
  • [...]

    链接地址: http://www.djcxy.com/p/17272.html

    上一篇: 我如何在Javascript中声明可选的函数参数?

    下一篇: 为什么typeof NaN返回'数字'?