Question discussion

What is the output of the following JavaScript code?console.log([1, 2, 3].includes(2), [NaN].includes(NaN), [NaN].indexOf(NaN));

2165Medium DSSSB TGT Computer Science 0 replies

What is the output of the following JavaScript code?

console.log([1, 2, 3].includes(2), [NaN].includes(NaN), [NaN].indexOf(NaN));
  • Atrue true 0
  • Btrue false -1
  • Ctrue true -1
  • Dtrue false 0
Correct answer: C

Explanation

includes() uses the SameValueZero comparison and therefore finds NaN, whereas indexOf() uses strict equality, for which NaN !== NaN, so it returns -1.

Discussion 0

Nobody has replied yet. Start the discussion!