Your product could be here, in front of thousands of JavaScript developers. Sponsor this spot →
JavaScript Quiz

Why does indexOf not find NaN but includes does?

Do you know what will be the output of this code?

JavaScript
console.log([NaN].indexOf(NaN), [NaN].includes(NaN));
Show answer and explanation

The answer is -1 true.

indexOf compares with ===, and NaN is never strictly equal to itself. includes uses the SameValueZero algorithm, which does match NaN.

Take the full quiz Next question →

More Arrays questions

  1. Why does [33, 2, 8].sort() not sort numbers correctly?
  2. What happens when you assign past the end of a JavaScript array?
  3. Why does [1, 2, 3].map(parseInt) return [1, NaN, NaN]?
  4. What does Array.push() return in JavaScript?
  5. How do you remove duplicates from an array with Set?

Related topics