Why is NaN === NaN false?

Ionică Bizău picture Ionică Bizău · Nov 13, 2013 · Viewed 27.3k times · Source

Why does NaN === NaN return false in Javascript?

> undefined === undefined
true
> NaN === NaN
false
> a = NaN
NaN
> a === a
false

On the documentation page I see this:

Testing against NaN

Equality operator (== and ===) cannot be used to test a value against NaN. Use isNaN instead.

Is there any reference that answers to the question? It would be welcome.

Answer

SLaks picture SLaks · Nov 13, 2013

Strict answer: Because the JS spec says so:

  • If Type(x) is Number, then
    • If x is NaN, return false.
    • If y is NaN, return false.

Useful answer: The IEEE 754 spec for floating-point numbers (which is used by all languages for floating-point) says that NaNs are never equal.