Skip to content
Related Articles
Open in App
Not now

Related Articles

JavaScript Array includes() Method

Improve Article
Save Article
Like Article
  • Difficulty Level : Basic
  • Last Updated : 15 Dec, 2022
Improve Article
Save Article
Like Article

The Javascript array.includes() method is used to know whether 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)

Parameters: 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 is an example of the Array includes() method.

Example: In this example, we will see if a value is included in an array or not using the Array includes() method.

javascript




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


Output:

true

 The below examples illustrate the Array includes() method in JavaScript:

Example 1: In this example, the method will search for element 2 in that array.

javascript




<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().
    console.log(a);
</script>


Output:

true

Example 2: In this example, the method will search for the element ‘cat’ in that array and returns false as ‘cat’ is not present in the array.

javascript




<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()
    console.log(a);
</script>


Output:

false

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

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

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.


My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!