How to test if a var is NaN in JavaScript

2015-04-29 | #javascript, #solution, #webdev

Well, there are two possibilities here, the first and common one is most likely not the reason you came to this page, but for reasons of completeness, here it is:


The problem with this: isNaN evaluates the expression and sees if the expression's value could be parsed into a number or not. This, of course, is highly unpractical if already have a variable and want to definitely make sure if it is NaN or not, not if it is a string that does not constitute a parsable number for example.

JavaScript weirdness to the rescue:

if( maybeNaN !== maybeNaN ){



Why does this work? Well, NaN is the _only_ value in JavaScript which in not identical to itself, because you can only tell that something is not a number, but not specifically what else. So you can use this little trick to check for NaN specifically.