1

I have met this strange issue.

enter image description here

Why is so?! Should it be so?

mskfisher
  • 3,177
  • 3
  • 33
  • 47
Andr
  • 607
  • 2
  • 9
  • 23

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
  • 121,982
  • 31
  • 284
  • 303
  • 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,122
  • 5
  • 19
  • 31
JohnB
  • 12,707
  • 4
  • 36
  • 64
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,661
  • 18
  • 31
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,122
  • 5
  • 19
  • 31
Marc B
  • 348,685
  • 41
  • 398
  • 480