Domanda

As it says in the question really, I tried value == NaN and it was false and then remembered that I should be using isNaN(value) to check for this.

Why the difference?

È stato utile?

Soluzione

both NaN == NaN and NaN === NaN evaluate to false as from MDN

NaN is a special value which you can think of as for example Infinity. Infinity is not equal to another Infinity as it has NO DEFINED VALUE.

Altri suggerimenti

I can't put it any better than MDN do so...

Unlike all other possible values in JavaScript, it is not possible to rely on the equality operators (== and ===) to determine whether a value is NaN or not, because both NaN == NaN and NaN === NaN evaluate to false. Hence, the necessity of an isNaN function.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top