I have a variable x and I want to test if x is set to NaN. How do I do that?
My first instinct is probably to, you know, test it, like this:
if (x === NaN) { ...
Silly rabbit, no, that would be far too easy. NaN is like NULL in SQL, it is not equal to anything, even itself.
But look, there is a function called isNaN()
-- maybe that will do it!
No, so far as I can tell, isNaN()
is utterly worthless.
For example, isNaN([""])
properly returns false, but isNaN(["."])
returns true. You don't want do know how I learned about this flaw.
How do I do this?
Turns out, my question is a duplicate of this one, but the selected answer is wrong. The right answer has 20% as many upvotes.
Short Answer
For ECMAScript-5 Users:
For people using ECMAScript-6:
And For consistency purpose across ECMAScript 5 & 6 both, you can also use this polyfill for Number.isNan
Note: I prefer to test using #1 which works same all places and does not have dependency on latest JS also. (It always gives me correct result. No surprises!)
Detailed Explanation:
Here is our awesome NaN
Please don't blame
JavaScript
for this, it is NaN which is supposed to behave this way in other languages also Which is fine as per rationale for all comparisons returning false NaN valuesSo comes
isNaN
as our savior, but wait it acts little differently in some scenarios likeI had some strange faces by seeing the results above. And here comes reason from MDN
So how should we test NaN for the non-numbers variables at all? I always go by the following
ECMAScript-6/JavaScript-2015 Updates
Do we have anything in ECMAScript-6 for the same. Yup we do...
The ES6 implementation will also be helpful for the above cases like
whereas ECMAScript-5 global function
isNaN
outputs intrue
for the above cases, which sometimes may not align with our expectation.The question has the answer if you read closely enough. That is the way I found it: I was typing out the question and... bingo.
You remember when I wrote "
NaN
is like NULL in SQL, it is not equal to anything, even itself"? So far as I know,NaN
is the only value in Javascript with this property. Therefore you can write: