CodePark/ questions / Equality & copying
START PRACTICINGPRACTICE
Frontend question · JavaScript › Equality & copying

What does this log?

console.log(
  NaN === NaN,
  Object.is(NaN, NaN),
  [NaN].includes(NaN),
  [NaN].indexOf(NaN)
);
false true true -1

The answer is false true true -1. NaN is the only value that is not strictly equal to itself. indexOf uses ===, so it cannot find NaN and returns -1.

Object.is uses the SameValue rule, and includes uses the SameValueZero rule. Both rules are like ===, but they treat NaN as equal to itself. (They differ only on 0 and -0: SameValue says they are different, SameValueZero says they are equal.)

The tempting option "false true false -1" is wrong because
includes does not use ===. It finds NaN.