search

Array.prototype.includes

Returns true if the array contains searchElement using SameValueZero equality (ES2016). Reach for it as the readable boolean alternative to `indexOf(x) !== -1`.

arr.includes(searchElement[, fromIndex])

Parameters

Parameter Purpose
searchElement Value to check for
fromIndex Index to start searching from; negative counts from the end

Examples

console.log([1,2,3].includes(2));

Logs: true

console.log([1,2,3].includes(9));

Logs: false

console.log([NaN, 1].includes(NaN));

Logs: true (unlike indexOf, includes matches NaN)

console.log([1,2,3].includes(1, 1));

Logs: false (starts search at index 1)

Gotcha

Uses SameValueZero, so NaN matches NaN — the one behavioral difference from indexOf. Still reference equality for objects.

Related methods

← All JS array methods · TypeScript vs JavaScript