- Learning JavaScript Data Structures and Algorithms
- Loiane Groner
- 104字
- 2021-08-27 18:41:19
ECMAScript 2016 - using the includes method
The includes method returns true if an element is found in the array, and false otherwise. The following code is an example of how to use this method:
console.log(numbers.includes(15)); console.log(numbers.includes(20));
In this example, the includes(15) will return true and includes(20) will return false because the element 20 does not exist in the numbers array.
It is also possible to pass a starting index where we want the array to start searching for the value:
let numbers2 = [7,6,5,4,3,2,1]; console.log(numbers2.includes(4,5));
The output from the preceding example will be false because the element 4 does not exist after position 5.