[X]

NaN Can Hide

luljs still works

One array method finds NaN, another swears it is not there.

In 2026: Still works. indexOf uses strict equality, and since NaN === NaN is false, it can never find NaN and returns -1. includes was added later using SameValueZero, which treats NaN as equal to NaN, so it returns true.

Where it came from: indexOf predates includes and uses strict equality, which NaN fails; includes uses the newer SameValueZero, which matches NaN. MDN's includes page notes the difference. MDN

<script>
document.write("<p><code>[NaN].indexOf(NaN)</code> is <b>" + [NaN].indexOf(NaN) + "</b></p>");
document.write("<p><code>[NaN].includes(NaN)</code> is <b>" + [NaN].includes(NaN) + "</b></p>");
document.write("<p>indexOf uses ===, includes uses SameValueZero.</p>");
</script>
sandboxed demo · breaks nothing but itselfrestart

More in luljs

all 63 in luljs › · the whole library ›