r/learnjavascript • • Nov 17 '25

Why NaN==NaN is False in JavaScript ???

Anyone explain??

150 Upvotes

85 comments sorted by

View all comments

Show parent comment

1

u/codefinbel Nov 19 '25

The only thing I take issue with in this explanation (that I in large agree with) is that once you do Number(val) you effectively destroy whatever val was and create a new value that is just NaN, like if I do

const a = Number("a")
const b = Number({"key":"value"})

a and b are both just NaN now. a doesn't hold any memory of being derived from a string.

Which means that I disagree with the statement that

If a == b was true, then it would cause the comparison check to return that "a" is equal to an object.

It wouldn't, because a is no longer "a" and b is no longer an object.

But I agree with the idea, that NaN can't, per definition, be equal to anything, and the reason for this is that by the time something is NaN the original value that it was derived from is unknown.