Question concerning NaN in JS

What is the difference between using:

if (zipInt == NaN) {


if (isNaN(zipInt)) {

Is the first example valid?
In MDN I read this but I did not understand it well:

NaN compares unequal (via ==,!=, ===, and !==) to any other value – including to another NaN value. Use Number.isNaN() or isNaN() to most clearly determine whether a value is NaN

Can you explain this to me in plainer words and with simple examples. Any external link is welcome.

With the first case you have, if your variable zipInt does happen to be NaN, it still wouldn’t be equal to NaN because no NaN is equal to another NaN. NaN !== NaN. You would have to use isNaN or Number.isNaN(). I know it is very weird, but it happens to be true.

1 Like

NaN is never equal to anything. There are methods available to determine if something is not a number, but even (NaN == NaN) will always be false.

1 Like

Nananana, nananana, hey ey ey, goodbye.
Sorry, after reading your comment the song is in my head.


NaN is the the program saying “I believe that this value should be a number, but all I know is that it isn’t a number I recognise”. The NaN value the program returns in that case can’t be equal to anything because the program can’t evaluate what it actually is. So someValue == NaN will never evaluate to true because the program cannot tell what NaN is. And NaN is not equal to NaN for the same reason.

To check is a value is NaN, you either use the Number.isNaN(someValue) function, or you can check if someValue != someValue: the only time it will not be equal to itself is if the program doesn’t know what it is, i.e. it is NaN.

And now thanks to you I have
nananana nananana nananana BATMAN!

1 Like

Oooo that’s a fun one!

This uses the Equality Operator:
if (zipInt == NaN) {

While this is calling a function:
if (isNaN(zipInt)) {