2

Why are these two different?

var x = NaN; //e.g. Number("e");
alert(isNaN(x)); //true (good)
alert(x == NaN); //false (bad)
peter n
  • 1,210
  • 13
  • 18
  • 14
    Consider `NaN === NaN // false`. Ducks aren't Bunnies `Ducks !== Bunnies`, but neither are a _Number_ so both are `NaN`, hence `NaN` can't be assumed equal to itself and therefore `==` and `===` must return `false`. – Paul S. Feb 20 '13 at 17:39
  • http://javascriptweblog.wordpress.com/2011/02/07/truth-equality-and-javascript/ – Almo Feb 20 '13 at 17:41
  • Consider for a second why they would have an `isNaN` function in the first place. – ChaosPandion Feb 20 '13 at 17:41
  • Equality operator (== and ===) cannot be used to test a value against NaN. Use isNaN instead. https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/NaN – Salman Feb 20 '13 at 17:41
  • Think of it this way: `isNaN()` exists *because* the `==` comparisons cannot work in the "common sense" way, according to the IEEE floating-point spec. – Pointy Feb 20 '13 at 17:45
  • There was a good discussion about checking for `NaN` here: http://stackoverflow.com/q/14772076/1249581. – VisioN Feb 20 '13 at 17:53
  • 1
    Good comments, but existence of isNaN doesn't imply that == doesn't work. Helper functions is a common place. – peter n Feb 20 '13 at 18:28

3 Answers3

1

The equality and inequality predicates are non-signaling so x = x returning false can be used to test if x is a quiet NaN.

Source

This is the rule defined in IEEE 754 so full compliance with the specification requires this behavior.

ChaosPandion
  • 77,506
  • 18
  • 119
  • 157
1

Nothing is equal to NaN. Any comparison will always be false.

In both the strict and abstract comparison algorithms, if the types are the same, and either operand is NaN, the result will be false.

If Type(x) is Number, then

  • If x is NaN, return false.
  • If y is NaN, return false.

In the abstract algorithm, if the types are different, and a NaN is one of the operands, then the other operand will ultimately be coerced to a number, and will bring us back to the scenario above.

Community
  • 1
  • 1
the system
  • 9,244
  • 40
  • 46
  • Really? Infinity looks like the same as NaN. How about Infinity == Infinity. How come it returns true? – allenhwkim Feb 20 '13 at 18:38
  • @bighostkim: *"Infinity looks like the same as NaN..."* I don't know what you mean by that. They're not the same. – the system Feb 20 '13 at 18:40
  • typeof Infinity is number, as the same as typeof NaN. As you said, the other operand will ultimately be coerced to a number. – allenhwkim Feb 20 '13 at 18:47
  • What I think is "NaN" does not have a value. so ==, which compares value, is meaningless. so is === to compare value and type. – allenhwkim Feb 20 '13 at 18:50
  • @bighostkim: If the *types* are different, the non-numeric operand will be coerced to a number. Since the types of `Infinity` and `NaN` are the same, it the becomes a simple value comparison, except that if either operand is `NaN` *(as in this example)*, the result will always be `false`. http://es5.github.com/#x11.9.3 – the system Feb 20 '13 at 18:50
  • ...both `==` and `===` take into consideration the types of the operands. It's just that the `==` will perform coercion until the types of the operands match, whereas the `===` will immediately return `false` if the types don't match. – the system Feb 20 '13 at 18:51
0

The following operations return NaN

The divisions 0/0, ∞/∞, ∞/−∞, −∞/∞, and −∞/−∞
The multiplications 0×∞ and 0×−∞
The power 1^∞
The additions ∞ + (−∞), (−∞) + ∞ and equivalent subtractions.
Real operations with complex results:

The square root of a negative number
The logarithm of a negative number
The tangent of an odd multiple of 90 degrees (or π/2 radians)
The inverse sine or cosine of a number which is less than −1 or greater than +1.

The following operations return values for numeric operations. Hence typeof Nan is a number. NaN is an undefined number in mathematical terms. ∞ + (-∞) is not equal to ∞ + (-∞). But we get that NaN is typeof number because it results from a numeric operation.

From wiki:

ppsreejith
  • 3,318
  • 2
  • 25
  • 27
  • Why the downvote, guys? Atleast tell me so that i can improve. :| – ppsreejith Feb 20 '13 at 17:49
  • I didn't down vote but it seems clear to me that you have not answered the question **why**. – ChaosPandion Feb 20 '13 at 17:57
  • @ChaosPandion, Isnt it obvious? The following operations return values for numeric operations hence typeof Nan is a number. NaN is an undefined number in mathematical terms. ∞ + (-∞) is not equal to ∞ + (-∞). But we get that NaN is typeof number because it results from a numeric operation. That's what i wrote. – ppsreejith Feb 20 '13 at 18:02
  • It really isn't obvious. You have to consider it from the perspective of a novice. – ChaosPandion Feb 20 '13 at 18:04
  • @ChaosPandion Hmmm, I guess so. Anyway updated my answer. – ppsreejith Feb 20 '13 at 18:06
  • You lost me at the the first infinity. – peter n Feb 20 '13 at 18:20