Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
84
rated 0 times [  85] [ 1]  / answers: 1 / hits: 63293  / 9 Years ago, mon, may 18, 2015, 12:00:00

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.


More From » nan

 Answers
3

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:



var reallyIsNaN = function(x) {
return x !== x;
};

[#66558] Saturday, May 16, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lelasamiraa

Total Points: 208
Total Questions: 99
Total Answers: 107

Location: Uzbekistan
Member since Tue, Nov 30, 2021
3 Years ago
;