JavaScript Array includes() Method

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

  • Example:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script>
        var name = [ 'gfg', 'cse', 'geeks', 'portal' ];
      
        a = name.includes('gfg')
      
        // Printing result of includes()
        document.write(a);
    </script>

    chevron_right

    
    

  • Output:
    true

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:

  • searchElement: This parameter holds the element which 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.

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



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

  • Example 1: In this example the method will searched for the element 2 in that array.
    Input : [1, 2, 3, 4, 5].includes(2);
    Output: true
  • Example 2: In this example the method will searched for the element 9 in that array.
    Input : [1, 2, 3, 4, 5].includes(9);
    Output: false

Code for the above method is provided below:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>

chevron_right


Output:

true

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>

chevron_right


Output:

false

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

  • Google Chrome 47.0
  • Microsoft Edge 14.0
  • Mozilla Firefox 43.0
  • Safari 9.0
  • Opera 34.0

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : nidhi_biet, Akanksha_Rai