0

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?

Fiona - myaccessible.website
  • 14,481
  • 16
  • 82
  • 117
  • 2
    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 - see [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN) for more info – naththedeveloper Feb 21 '14 at 11:49
  • Check this out: http://stackoverflow.com/questions/14986361/why-is-isnanx-different-from-x-nan-where-x-nan – Nicolae Olariu Feb 21 '14 at 11:52
  • Anyone care to explain the downvote? – Fiona - myaccessible.website Feb 21 '14 at 13:07
  • possible duplicate of [What is the rationale for all comparisons returning false for IEEE754 NaN values?](http://stackoverflow.com/questions/1565164/what-is-the-rationale-for-all-comparisons-returning-false-for-ieee754-nan-values) – Qantas 94 Heavy Mar 13 '14 at 11:53

2 Answers2

5

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.

lukas.pukenis
  • 13,057
  • 12
  • 47
  • 81
  • 1
    Awesome comparison kool, solid one – Triode Feb 21 '14 at 11:52
  • 1
    You might want to make it clear that you're referring to the mathematical concept of infinities, not the `Infinity` variable in JavaScript (as `Infinity === Infinity` in JavaScript). – Qantas 94 Heavy Apr 12 '14 at 13:28
3

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.

naththedeveloper
  • 4,503
  • 6
  • 36
  • 44