JavaScript Tutorials

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:

1.
includes(searchElement)

Parameters

ParameterOptional/RequiredDescription
searchElementrequiredThe 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.

2.
includes(searchElement, fromIndex)

Parameters

ParameterOptional/RequiredDescription
searchElementrequiredThe value to search for in the array.
fromIndexoptionalThe 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,

  1. We define an array variable arr with elements [1, 2, 3, 4, 5].
  2. We use the includes() method with the value 3 to check if the array contains the value 3.
  3. The result is stored in the variable containsThree.
  4. 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,

  1. We define an array variable arr with elements [1, 2, 3, 4, 5].
  2. 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.
  3. The result is stored in the variable containsThreeFromIndex3.
  4. 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,

  1. We define an array variable strArr with elements ['apple', 'banana', 'cherry'].
  2. We use the includes() method with the value 'banana' to check if the array contains the string 'banana'.
  3. The result is stored in the variable containsBanana.
  4. 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.