+ 1
Puzzling
why does the following expression evaluate to True. console.log (NaN! == NaN);
2 Respostas
+ 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.
+ 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