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
Array.prototype.indexOf
Returns the first index where searchElement is strictly equal (===) to an array item, or -1. Reach for it for a fast, exact primitive lookup — but not for objects or NaN.
Array.prototype.some
Returns true if the callback returns truthy for at least one element, short-circuiting on the first hit. Reach for it to answer "does anything match?" without walking the full array.
Array.prototype.find
Returns the first element for which the callback returns truthy, or undefined if none match. Reach for it when you need the matching item itself, not just its index.
Array.prototype.findIndex
Returns the index of the first element for which the callback returns truthy, or -1 if none match. Reach for it when you need the position — e.g. to splice or replace.
Array.prototype.findLast
Walks the array from the END and returns the last element for which the callback returns truthy (ES2023). Reach for it when the most recent matching entry is what you want.