Array.prototype.includes()
Determines whether an array contains a given value.
Syntax
arr.includes(searchElement, fromIndex) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
searchElement | any | Yes | The value to search for. |
fromIndex | number | No | Index to start searching from. Negative values count from the end. Defaults to 0. |
Returns
boolean — true if the value is found; otherwise false.
Examples
console.log([1, 2, 3].includes(2));
Output
true
console.log([1, NaN].includes(NaN));
Output
true
Notes
Uses SameValueZero comparison, so it can find NaN (unlike `indexOf`). Does not
work for matching object contents - only reference equality for objects.
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | 47 |
| firefox | 43 |
| safari | 9.0 |
| edge | 14 |
| node | 6.0 |