Puzzling | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 1

Puzzling

why does the following expression evaluate to True. console.log (NaN! == NaN);

5th Apr 2018, 3:57 PM
Tim Millar
Tim Millar - avatar
2 Réponses
+ 1
NaN compares unequal (via ==, !=, ===, and !==) to any other value -- including to another NaN value. Use Number.isNaN() or isNaN() to most clearly determine whether a value is NaN. Or perform a self-comparison: NaN, and only NaN, will compare unequal to itself. Direct form MDN( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN ) Not a reason but an acknowledgment of the quirk and some alternatives.
5th Apr 2018, 4:40 PM
ODLNT
ODLNT - avatar
+ 1
thanks for a well thought out answer and the link explaining it. Here is more food for thought on NaN. NaN === NaN; // false Number.NaN === NaN; // false isNaN(NaN); // true isNaN(Number.NaN); // true function valueIsNaN(v) { return v !== v; } valueIsNaN(1); // false valueIsNaN(NaN); // true valueIsNaN(Number.NaN); // true
5th Apr 2018, 7:06 PM
Tim Millar
Tim Millar - avatar