Open In App

JavaScript Array includes() Method

Improve
Improve
Like Article
Like
Save
Share
Report

JavaScript Array includes() method that checks if an array contains a specific element. It returns true if the element is found, and false otherwise. It’s useful for determining the presence of elements without needing to loop through the array.

Array includes() Method Syntax

array.includes(searchElement, start);

Array includes() Method Parameters

  • searchElement: This parameter holds the element that will be searched.
  • start: This parameter is optional and it holds the starting point of the array, where to begin the search the default value is 0.

Array includes() Method Return Value

It returns a Boolean value i.e., either True or False.

Array includes() Method Examples

Example 1: Searching for number in an array

Here, the method will search for element 2 in that array.

javascript




// Taking input as an array A
// having some elements.
let A = [1, 2, 3, 4, 5];
 
// includes() method is called to
// test whether the searching element
// is present in given array or not.
a = A.includes(2)
 
// Printing result of includes().
console.log(a);


Output

true


Explanation

This code demonstrates the use of the includes() method to check if the element ‘2’ is present in the array A. The method returns true because ‘2’ is found in the array.

Example 2: Searching for a string in an array

Here, the method will search for the element ‘cat’ in that array and returns false as ‘cat’ is not present in the array.

javascript




// Taking input as an array A
// having some elements.
let name = ['gfg', 'cse', 'geeks', 'portal'];
 
// includes() method is called to
// test whether the searching element
// is present in given array or not.
a = name.includes('cat')
 
// Printing result of includes()
console.log(a);


Output

false


Explanation

This code demonstrates the use of the includes() method to check if a specific element (‘cat’) is present in the array name. The method returns false because ‘cat’ is not found in the array.

We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete referencearticle.

Supported Browsers:



Last Updated : 05 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads