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.
typeof NaN
returns'number'
because:ECMAScript spec says the Number type includes NaN:
So
typeof
returns accordingly:This behavior is in accordance with IEEE Standard for Floating-Point Arithmetic (IEEE 754):
We could argue that NaN is a special case object. In this case, NaN's object represents a number that makes no mathematical sense. There are some other special case objects in math like INFINITE and so on.
You can still do some calculations with it, but that will yield strange behaviours.
More info here: http://www.concentric.net/~ttwang/tech/javafloat.htm (java based, not javascript)
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, butNaN
is a special case).A specific
NaN
is not considered equal to anotherNaN
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:
All this means is (broken down into parts):
Basically, a
NaN
is not equal to any other number, including anotherNaN
, and even including itself.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).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
, thenx == x
will always be true. Ifx
is aNaN
, thenx == x
will always be false. It's giving you a way to detectNaN
easily (quietly).Because NaN is a numeric data type.
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.
It means Not a Number. It is not a peculiarity of javascript but common computer science principle.
From http://en.wikipedia.org/wiki/NaN:
All these values may not be the same. A simple test for a NaN is to test
value == value
is false.