1

NaN (Not a Number).

  1. Why does not NaN equal(==) or strict equal(===) to NaN itself?
  2. How we can evaluate the following conditions to true?
let x = NaN;
// equal(==)
if(x == NaN) {
    console.log(`${x} (Not a Number)`);
}
// strict equal(===)
if(x === NaN) {
    console.log(`${x} (Not a Number)`);
}
  • It is same as infinity != infinity concept I think – Sanooj T Jan 05 '22 at 10:33
  • Here is a simple example why `NaN` doesn't equal itself. It's a placeholder value for *something* that was turned into a number but...cannot be represented as a number. Something that works would be `Number("123")` this converts cleanly to the number `123`. However, for example `Number("apple")` does not and produces `NaN`. As does `Number("orange")`. Now, clearly `"apple" === "orange"` is `false`. So why would `Number("apple") === Number("orange")` be `true`? In both cases they are `NaN` as number but the original non-numeric value is not the same. – VLAZ Jan 05 '22 at 10:35
  • 1
    @SanoojT Important to note that mathematically infinities are not equal (simple example are *all integers* equal to *all even integers* - both are infinite but aren't equal). However in JS `Infinity === Infinity`. Which is iffy mathematically but it's how the value works. – VLAZ Jan 05 '22 at 10:37

0 Answers0