Array.prototype.includes
Checks whether an array contains a value.
Available since: ES2016
Syntax
array.includes(searchElement, fromIndex)Parameters
| Parameter | Type | Description |
|---|---|---|
| searchElement | any | The value to look for. |
| fromIndex | number | Optional. The index to start searching from. |
Return value
boolean`true` if the value is found, `false` otherwise.
Examples
Looking for a value:
console.log(['a', 'b', 'c'].includes('b'));
console.log(['a', 'b', 'c'].includes('z'));Output:
true
falseThe comparison is strict: the string '1' does not equal the number 1:
console.log([1, 2, 3].includes('1'));Output:
falseNotes
Objects are compared by reference, so the method cannot find an object with the same properties — use some() or find() for that.
See also
- Array.prototype.find — Returns the first element that passes a test.
- Array.prototype.some — Checks whether at least one element passes a test.
- Array.prototype.indexOf — Finds a value in an array and returns its index.
- Set.prototype.has — Checks whether a value is in a Set.
Related recipes
In the tutorial
- Searching arrays — How to find an element in a JavaScript array: includes, indexOf, find, findIndex, some and every.