There are several different ways of checking if a value is NaN , but unfortunately most of them are unreliable.
Let's walk through some of them and see how they fail. What's going on here? In JavaScript, NaN has the distinction of being the only value that is not equal to itself. That means we can't find out whether a value is NaN by checking equality to NaN because the answer will always be no. This one seems to work. And in fact it does work under some circumstances. However, despite the name, the purpose of the isNaN function isn't to check whether a value is NaN.
Waite Yes, but lots of people come here when Googling something like "how to check if a variable is nan in javascript". Waite I agree with you, but I have the same concern as Gothdo has.
You can check mjohnsonengr's comment on the same answer. BTW, I too have flagged This question for moderators attention. Show 3 more comments. Active Oldest Votes. Improve this answer. Community Bot 1 1 1 silver badge. Is there a need for paseFloat? If you want an empty string to be interpreted as NaN , then yes.
If i were you i would assign it to a variable and , use condition variable! As it is laid out in specs tc This could also be useful as to try out w3schools. Add a comment. Jazzy Jazzy 5, 9 9 gold badges 46 46 silver badges 71 71 bronze badges. Effective but counterintuitive. I wouldn't want to maintain this kind of code.
It's not the code that's counterintuitive, it's NaN. It's how it's implemented in CPUs, and it's the expected behavior in every programming language in the world that uses floats. Tarmil Duly noted! That's actually really interesting; I always thought NaN inequality weirdness was a Javascript thing. This is a known bug in javascript. Show 8 more comments. NickPineda Why? Thanks for not just using "isNan " but rather providing a completely detailed answer.
FelixKling Please check the updated answer. The resulting value is then tested to determine whether it is NaN. NaN doc at MDN — robsch. Jonathan Jonathan 2, 3 3 gold badges 22 22 silver badges 35 35 bronze badges.
That's not a perfectly reliable way. As per the specs. The same can be said for null. So rather than this: parseFloat '1. While chiborg 's answer IS correct, there is more to it that should be noted: parseFloat '1.
Ryan Griffith Ryan Griffith 1, 15 15 silver badges 37 37 bronze badges. Any way to counter this and detect that "1. Note that this happens only when the string begins with a number. So isNaN cannot help. Then what should we do instead? Yuci Yuci The section 3.
Operations resulting in NaN details into the operations that generate NaN. Thus the above snippet logs to console "Is NaN". The difference between these functions is that Number. Thus the function returns true. On the other side, Number. For example, you could easily transform the '1. Even with Number. Alternatively, in the absence of Number. A polyfill for isNaN would be the polyfill leverages the unique never-equal-to-itself characteristic of NaN :. There is a more usage oriented way to think of isNaN : If isNaN x returns false , you can use x in an arithmetic expression not making the expression return NaN.
If it returns true , x will make every arithmetic expression return NaN.
0コメント