JavaScript Array includes() Method
Example
Check the array site Whether it contains runoob:
let
site
=
[
'
runoob
'
,
'
'
,
'
taobao
'
]
;
site
.
includes
(
'
runoob
'
)
;
//
true
site
.
includes
(
'
baidu
'
)
;
//
false
Try it »
Definition and Usage
The includes() method is used to determine whether an array contains a specified value, returning true if it does, otherwise 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 indicate the first browser version that supports this method.
| Method | |||||
|---|---|---|---|---|---|
| includes() | 47 | 14 | 43 | 9 | 34 |
Syntax
arr.includes(searchElement)
arr.includes(searchElement, fromIndex)
Parameter Description
| Parameters | Description |
|---|---|
| searchElement | Required. The element value to search for. |
| fromIndex | Optional. The index at which to start searching for searchElement. If negative, the search starts at 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 is greater than or equal to the array length
If fromIndex is greater than or equal to the array length, it returns false. The array will not be searched:
Example
var
arr
=
[
'
a
'
,
'
b
'
,
'
c
'
]
;
arr
.
includes
(
'
c
'
,
3
)
;
//
false
arr
.
includes
(
'
c
'
,
100
)
;
//
false
The computed index is less than 0
If fromIndex is negative, the computed index is used as the position to start 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