The Wayback Machine - https://web.archive.org/web/20220429034919/https://www.geeksforgeeks.org/javascript-array-includes-method/amp/

JavaScript Array includes() Method

Below is the example of the Array includes() method.

The array.includes() method is used to know either a particular element is present in the array or not and accordingly, it returns true or false i.e, if the element is present, then it returns true otherwise false.

Syntax:

array.includes(searchElement, start)

Parameter: This method accepts two parameters as mentioned above and described below:

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



Below examples illustrate the Array includes() method in JavaScript:

Code for the above method is provided below:

Program 1:




<script>
  
    // Taking input as an array A
    // having some elements.
    var 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().
    document.write(a);
</script>

Output:

true

Program 2:




<script>
    // Taking input as an array A
    // having some elements.
    var 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()
    document.write(a);
</script>

Output:

false

Supported Browsers: The browsers supported by JavaScript Array includes() method are listed below:




Article Tags :