Not why, but HOW is NaN not equal to NaN in Javascript?

899 views javascript

I understand the reasoning behind this. I'm curious about the technical implementation. Having trouble finding this somewhere.

My theory is that as soon as the left NaN is evaluated in any comparison, it automatically returns false without performing the comparison at all. Is this correct?

answered question

"How is NaN not equal to NaN in JavaScript" - Simple, NaN simply means not a number. Take this example: Math.sqrt(-1) results in NaN. parseInt("car") also results in NaN. In no way shape or form are they remotely equal, but rather they are represented by NaN because neither one are numbers. NaN does not imply a type but rather only explicitly states that the value is not a number. It could be any number of types, but the conversion to the type of Number failed and therefore the returned value is not a number...

@MichaelJasper OP is specifically asking how the logic is implemented, not why - "What is the rationale" is about "why", not "how".

I was looking more for the accepted answer, which shows the evaluation step by step from start to finish.

@War10ck I'm not sure where you got that but it's completely false. NaN is actually a number.

1 Answer


Yes - if both types are the same, and they are numbers, then if the left one is NaN, then the result is false, without checking the value of the right one:

7.2.13 Strict Equality Comparison

The comparison x === y, where x and y are values, produces true or false. Such a comparison is performed as follows:

If Type(x) is different from Type(y), return false.

If Type(x) is Undefined, return true.

If Type(x) is Null, return true.

If Type(x) is Number, then

  • If x is NaN, return false.

  • If y is NaN, return false.

  • If x is the same Number value as y, return true.

  • If x is +0 and y is ?0, return true.

  • If x is ?0 and y is +0, return true.

  • Return false.


posted this

Have an answer?


Please login first before posting an answer.