JavaScript Array includes()
Syntax & Examples
Array.includes() method
The includes() method of the Array class in JavaScript determines whether the calling array contains a specified value, returning true or false as appropriate.
Syntax of Array.includes()
There are 2 variations for the syntax of Array.includes() method. They are:
includes(searchElement)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
searchElement | required | The value to search for in the array. |
This method determines whether the calling array contains the specified value, starting the search at the beginning of the array.
Returns value of type Boolean
.
includes(searchElement, fromIndex)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
searchElement | required | The value to search for in the array. |
fromIndex | optional | The position in the array at which to begin the search. Defaults to 0. |
This method determines whether the calling array contains the specified value, starting the search at the specified fromIndex.
Returns value of type Boolean
.
✐ Examples
1 Using includes() method to check for a value in an array
In JavaScript, we can use the includes() method to check if an array contains a specified value.
For example,
- We define an array variable arr with elements [1, 2, 3, 4, 5].
- We use the includes() method with the value 3 to check if the array contains the value 3.
- The result is stored in the variable containsThree.
- We log containsThree to the console using console.log() method to see if the array contains the value 3.
JavaScript Program
const arr = [1, 2, 3, 4, 5];
const containsThree = arr.includes(3);
console.log(containsThree);
Output
true
2 Using includes() method to check for a value starting from a specific index
We can use the includes() method to check if an array contains a specified value, starting the search from a specific index.
For example,
- We define an array variable arr with elements [1, 2, 3, 4, 5].
- We use the includes() method with the value 3 and the fromIndex 3 to check if the array contains the value 3 starting from index 3.
- The result is stored in the variable containsThreeFromIndex3.
- We log containsThreeFromIndex3 to the console using console.log() method to see if the array contains the value 3 starting from index 3.
JavaScript Program
const arr = [1, 2, 3, 4, 5];
const containsThreeFromIndex3 = arr.includes(3, 3);
console.log(containsThreeFromIndex3);
Output
false
3 Using includes() method to check for a string value in an array
We can use the includes() method to check if an array contains a specified string value.
For example,
- We define an array variable strArr with elements ['apple', 'banana', 'cherry'].
- We use the includes() method with the value 'banana' to check if the array contains the string 'banana'.
- The result is stored in the variable containsBanana.
- We log containsBanana to the console using console.log() method to see if the array contains the string 'banana'.
JavaScript Program
const strArr = ['apple', 'banana', 'cherry'];
const containsBanana = strArr.includes('banana');
console.log(containsBanana);
Output
true
Summary
In this JavaScript tutorial, we learned about includes() method of Array: the syntax and few working examples with output and detailed explanation for each example.