Is NaN equal to NaN?

joseph picture joseph · Aug 8, 2011 · Viewed 45.7k times · Source
parseFloat("NaN")

returns "NaN", but

parseFloat("NaN") == "NaN"

returns false. Now, that's probably a good thing that it does return false, but I don't understand how this is so. Did the JavaScript creators just make this a special case? Because otherwise I can't understand how this returns false.

Answer

Phil picture Phil · Aug 8, 2011

When a JavaScript function returns NaN, this is not a literal string but an object property in the global space. You cannot compare it to the string "NaN".

See https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/NaN