Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is NaN === NaN false? [duplicate]

Tags:

javascript

nan

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.

like image 846
Ionică Bizău Avatar asked Nov 13 '13 14:11

Ionică Bizău


People also ask

Why is NaN === NaN false?

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.

Does NaN equal false?

to ECMAScript code, all NaN values are indistinguishable from each other. Yes, it's just that your answer could be read as the value being the string "Not-a-Number" rather than a value that represents not being a number.

How do you know if NaN is equality?

In JavaScript, the best way to check for NaN is by checking for self-equality using either of the built-in equality operators, == or === . Because NaN is not equal to itself, NaN != NaN will always return true .

Is NaN less than 0 JavaScript?

NaN is neither less than nor greater than any number.


1 Answers

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.

like image 190
SLaks Avatar answered Sep 23 '22 06:09

SLaks