1

I have met this strange issue.

enter image description here

Why is so?! Should it be so?

mskfisher
  • 3,291
  • 4
  • 35
  • 48
Andr
  • 617
  • 2
  • 9
  • 24
  • 1
    Please add some text to the question, or ask your question at `imgur`... :) – gdoron May 28 '12 at 17:03
  • 3
    possible duplicate of [Javascript variable / 'NaN' behaving weirdly](http://stackoverflow.com/questions/10083937/javascript-variable-nan-behaving-weirdly) – Josh Lee May 28 '12 at 17:12

4 Answers4

12

From the MDN docs for isNaN:

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.

use isNaN instead.

Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307
  • 1
    Intuitively: 3 is equal to 3, 4 is equal to 4, but non-number (say, "apple") is not necessarily equal to non-number (say, "orange"). – Amadan May 28 '12 at 17:12
5

The reason behind this is that the rules of mathematics should be preserved. Otherwise, one would have x == x + 1 if x is NaN, which is not a true relationship for any other value of x.

ata
  • 3,398
  • 5
  • 20
  • 31
JohnB
  • 13,315
  • 4
  • 38
  • 65
3

that's why we use

isNaN(x)

it seems that x is a NaN object and it does not compare equal to another

neu-rah
  • 1,662
  • 19
  • 32
2

NaN is like an SQL null. It is never equal to anything, including itself. That's why there's the special case function isNaN() to safely test for NaN's presence.

ata
  • 3,398
  • 5
  • 20
  • 31
Marc B
  • 356,200
  • 43
  • 426
  • 500