Why when compare NaN it return always false ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why when compare NaN it return always false ?

var n1 = 5 * "hossam"; var n2 = 5 * "hossam"; console.log(n1 === n2); //false console.log(n1 == n2); // false Why ???

10th Feb 2019, 10:11 AM
Hossam Hassan
Hossam Hassan - avatar
4 Answers
+ 5
In terms of accepting NaN != NaN, something which is not a number is not necessarily equivalent to something else which also isn't a number. All in all, like Jomari pointed out, NaN is just designed to fail the equality test since NaN does not refer to anything specific.
10th Feb 2019, 11:32 AM
Hatsy Rei
Hatsy Rei - avatar
+ 3
n1 and n2 produce NaN as because you multiple it with not a number value(NaN) NaN is supposed to denote the result of a nonsensical computation, and as such, it isn’t equal to the result of any other nonsensical computations. NaN is only one value in JavaScript that is not equal to itself!!
29th Dec 2020, 7:25 AM
Nilesh Roy
Nilesh Roy - avatar
+ 2
That's just the way how it was configured. NaN will fail every equality check to any value including to itself.
10th Feb 2019, 10:35 AM
Jomari Pantorilla
Jomari Pantorilla - avatar
+ 1
I won't use it for compare but all what I want know is what behind seen ? ... for ex: int x = new Int(5); Int y = new Int(5); Both not equal coz different in memory address ... etc
10th Feb 2019, 12:24 PM
Hossam Hassan
Hossam Hassan - avatar