Why is NaN === NaN false?

JavascriptNan

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

This behaviour is specified by the IEEE-754 standard (which the JavaScript spec follows in this respect).

For an extended discussion, see https://stackoverflow.com/questions/1565164/what-is-the-rationale-for-all-comparisons-returning-false-for-ieee754-nan-values

Solution 3 - Javascript

Although either side of NaN===NaN contains the same value and their type is Number but they are not same. According to ECMA-262, either side of == or === contains NaN then it will result false value.

you may find a details rules in here-

http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIonică BizăuView Question on Stackoverflow
Solution 1 - JavascriptSLaksView Answer on Stackoverflow
Solution 2 - JavascriptNPEView Answer on Stackoverflow
Solution 3 - JavascriptMd Mehedi HasanView Answer on Stackoverflow