Try it: In Node.js, or Firefox, open a REPL, and type:
Number.isNaN('asdf');
How can this not be NaN
? What is NaN
if not this?
Try it: In Node.js, or Firefox, open a REPL, and type:
Number.isNaN('asdf');
How can this not be NaN
? What is NaN
if not this?
- As an aside, I highly remend this great JSConf talk - " Everything you never wanted to know about JavaScript numbers -- JSConf EU 2013" youtube./watch?v=MqHDDtVYJRI It explains the low-level (Bit-level) details of how "Number" is implemented in JS but in a way that anyone can understand it. – Mörre Commented Nov 24, 2013 at 10:57
-
Number.isNaN(Number('asdf'))
– zamnuts Commented Nov 24, 2013 at 11:21
5 Answers
Reset to default 3You have a misconception here. The fact that NaN
means Not a Number does not mean that anything that is not a number is a NaN
.
NaN
is a special value on floating point arithmethic that represents an undefined result of an operation. For example, 0 / 0
generally yields NaN
as a result.
More information here and here.
There is no notion of Number.isNaN()
in the language specification. The only specified function is the global isNaN
(15.1.2.4), which will correctly return true
on anything that isn't a number or cannot get correctly transformed into a number.
Number.isNaN
(which returns true on NaN
) is probably an implementation detail part of the next standard draft (see remyabel' answer) which could be used internally by isNaN
.
Just a note, it seems that Number.IsNaN
has limited support.
This output:
console.log(Number.isNaN("blabla"));
Gives false
, and false !== NaN
:
console.log(false !== NaN)
console.log(false !== Number.NaN);
Here's a reference:
When the Number.isNaN is called with one argument number, the following steps are taken:
If Type(number) is not Number, return false. If number is NaN, return true. Otherwise, return false.
This function differs from the global isNaN function (18.2.3) is that it does not convert its argument to a Number before determining whether it is NaN.
I am l33t for I have 1337 reputation.
Try this:
function checkNaN(value)
{
return value !== value;
}
It works because (I believe) NaN is the only thing in JS that does not strictly equal itself.
This is part of the EcmaScript 6 Standard.
Number.isNaN
returns whether a value is NaN without coercing the parameter.
Number.isNaN("xmfds"); // false
Number.isNaN("ose"); // false
isNaN
returns whether the value is NaN after coercing the parameter to Number
type.
isNaN("ose") -> isNaN( Number("ose") ) -> isNaN(NaN); // true