4

Can you explain the logical evaluation of NaN in javascript?

NaN==NaN   (false)
!NaN==!NaN (true)
NaN!=NaN   (true)
NaN==false (false)
NaN==true  (false)

I am a little surprised here...

Jan Turoň
  • 31,451
  • 23
  • 125
  • 169

1 Answers1

3

Because that's what the spec says.

Specifically, the IEE floating point spec says that NaN does not equal itself.
Therefore, in Javascript, there is no value that NaN can ever equal.

!NaN is true, because NaN is a falsy value.

This makes all of your examples obvious.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • OK, but how should I comprehend the negation of such value? My brain hurts... – Jan Turoň May 26 '14 at 17:25
  • And it also implies !NaN == false (false) and !NaN == true (true) what's true – jean May 26 '14 at 17:25
  • @JanTuroň: The result of using `!` is a Boolean value. So if you use `!NaN==!NaN `, then you are comparing to Booleans with each other, not `NaN`. If that's what you meant. Converting a value to a Boolean and comparing a value to another value are two different things. Only because `!NaN` is `true` doesn't mean that `NaN == false` (that's because `NaN` is not converted to a Boolean in this process). – Felix Kling May 26 '14 at 17:37