JavaScript (ES2026) reference · Array Methods
JavaScript Array includes() Method
Return true if the array contains a value, using SameValueZero equality (so it finds NaN, unlike indexOf).
JavaScript (ES2026) Since ES2016
Syntax
array.includes(searchElement, fromIndex)
Parameters
| Parameter | Description |
|---|---|
searchElement |
The value to look for. |
fromIndex optional |
Index to start searching from. Default: 0 |
Return value
true or false.
Examples
const pets = ["cat", "dog"];
console.log(pets.includes("dog"), pets.includes("bird"));
Output
true false
console.log([NaN].includes(NaN), [NaN].indexOf(NaN));
Output
true -1
const users = [{ id: 1 }];
console.log(users.includes({ id: 1 })); // different object
console.log(users.some((u) => u.id === 1));
Output
false true
Version history
| Version | Change |
|---|---|
| JavaScript ES2016 | Added |
Related
array.indexOf()· Return the first index at which a value is found (using ===), or -1 if it isn't in the array.array.some()· Return true if at least one element passes a test function. Stops at the first match; an empty array returns false.array.find()· Return the first element that passes a test function, or undefined if none does.- JavaScript Arrays · tutorial
- JavaScript Array Methods · tutorial