JavaScript Array includes() Method
Example
Detect arraysiteWhether it contains example:
let site = ['example', 'google', 'taobao'];
site.includes('example');
// true
site.includes('baidu');
// false
Try it Yourself »
Definition and Usage
The includes() method is used to determine whether an array contains a specified value. If it does, it returns true; otherwise, it returns false.
[1, 2, 3].includes(2); // true [1, 2, 3].includes(4); // false [1, 2, 3].includes(3, 3); // false [1, 2, 3].includes(3, -1); // true [1, 2, NaN].includes(NaN); // true
Browser Support
The numbers in the table specify the first browser version that supports the method.
| Method | |||||
|---|---|---|---|---|---|
| includes() | 47 | 14 | 43 | 9 | 34 |
Syntax
arr.includes(searchElement) arr.includes(searchElement, fromIndex)
Parameter Description
| Parameter | Description |
|---|---|
| searchElement | Required. The element value to search for. |
| fromIndex | Optional. The index at which to begin searching for searchElement. If negative, the search starts at the index array.length + fromIndex in ascending order. Default is 0. |
Technical Details
| Return value: | Boolean. Returns true if the specified value is found, otherwise returns false. |
|---|---|
| JavaScript Version: | ECMAScript 6 |
More Examples
fromIndex greater than or equal to array length
If fromIndex is greater than or equal to the array length, false is returned. The array will not be searched:
Example
var arr = ['a', 'b', 'c'];
arr.includes('c', 3); //false
arr.includes('c', 100); // false
Computed index less than 0
If fromIndex is negative, the computed index is used as the position at which to begin searching for searchElement. If the computed index is less than 0, the entire array will be searched.
Example
//The array length is 3
//fromIndex is -100
//computed index is 3 + (-100) = -97
var arr = ['a', 'b', 'c'];
arr.includes('a', -100); // true
arr.includes('b', -100); // true
arr.includes('c', -100); // true
JavaScript Array Object