The JavaScript array includes() method checks whether the given array contains the specified element.
The includes()
method is case sensitive.
Blank spaces are also taken into account when searching a String or an Array.
JavaScript Array includes() method returns true if an array contains the element, otherwise false.
Syntax
array.includes(element, start)
This method accepts two parameters as mentioned above and described below:
element – Required. The value to be searched.
start – Optional. Start position. Default is 0. It represents the index from where the method starts search.
Return Value
A boolean value.
The includes()
method returns true
if an array contains a specified value.
The includes()
method returns false
if the value is not found.
Examples
Below are the examples of the Array includes() method.
const array1 = [1, 2, 3];
console.log(array1.includes(2));
// expected output: true
const pets = ['cat', 'dog', 'bat'];
console.log(pets.includes('cat'));
// expected output: true
console.log(pets.includes('at'));
// expected output: 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
["1", "2", "3"].includes(3) // false
const furniture = ['table', 'chair', 'cupboard', 'wardrobe', 'stool'];
console.log(furniture.includes('chair'));
// Expected output: true