JavaScript | String includes() Method

In JavaScript, includes() method determines whether a string contains the given characters within it or not.
This method returns true if the string contains the characters, otherwise, it returns false.
Note: The includes() method is case sensitive i.e, it will treat the Uppercase characters and Lowercase characters differently.

Syntax:

string.includes(searchvalue, start)

Parameters Used:

  • search value: It is the string in which the search will take place.
  • start: This is the position from where the search will be processed
    (although this parameter is not necessary if this is not mentioned
    the search will begin from the start of the string).
  • Return Value:

  • Returns either a Boolean true indicating the presence or it returns a false indicating the absence.

Examples:

Input : Welcome to GeeksforGeeks.
        str.includes("Geeks");

Output : true

Explanation : Since the second parameter is not defined, the search will take place from the starting index. And it will search for Geeks, as it is present in the string, it will return a true.

Input: Welcome to GeeksforGeeks.

Output: false

Explanation : Even in this case the second parameter is not defined, so the search will take place from the starting index. But as this method is case sensitive it will treat the two strings differently, hence returning a boolean false.
Since it is case sensitive.
Codes for the above function are provided below.

Code 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<body>
<p id="GFG"></p>
<script>
  
    var str = "Welcome to GeeksforGeeks.";
    var check = str.includes("Geeks");
    document.getElementById("GFG").innerHTML = check;
  
</script>
  
</body>
</html>

chevron_right


Output : false

Code 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<body>
<p id="GFG"></p>
<script>
  
    var str = "Welcome to GeeksforGeeks.";
    var check = str.includes("geeks");
    document.getElementById("GFG").innerHTML = check;
  
</script>
  
</body>
</html>

chevron_right


Output : false

Code 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<body>
<p id="GFG"></p>
<script>
  
    var str = "Welcome to GeeksforGeeks.";
    var check = str.includes("o", 17);
    document.getElementById("GFG").innerHTML = check;
  
</script>
</body>
</html>

chevron_right


Output : true

Code 4:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<body>
<p id="GFG"></p>
<script>
  
    var str = "Welcome to GeeksforGeeks.";
    var check = str.includes("o", 18);
    document.getElementById("GFG").innerHTML = check;
  
</script>
  
</body>
</html>

chevron_right


Output : false

Exceptions :

  1. The search will not be processed if the second parameter i.e computed index(starting index) is greater than or equal to the string length and hence return false.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
    <body>
    <p id="GFG"></p>
    <script>
      
        var str = "Welcome to GeeksforGeeks.";
        var check = str.includes("o", 25);
        document.getElementById("GFG").innerHTML = check;
      
    </script>
      
    </body>
    </html>

    chevron_right

    
    

    Output : false
    
  2. If the computed index(starting index) i.e the position from which the search will begin
    is less than 0, the entire array will be searched.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
    <body>
    <p id="GFG"></p>
    <script>
      
        var str = "Welcome to GeeksforGeeks.";
        var check = str.includes("o", -2);
        document.getElementById("GFG").innerHTML = check;
      
    </script>
      
    </body>
    </html>

    chevron_right

    
    

    Output : true
    


My Personal Notes arrow_drop_up

Lets get started

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.