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

JavaScript String includes() Method

Below is the example of the String includes() Method. 
 

Example: 
 

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!




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

Output: 

true

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: 
 

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: 
 




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

Output: 
 

false

Code 2: 
 




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

Output: 
 

false

Code 3: 
 






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

Output: 
 

true

Code 4: 
 




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

Output: 
 

false

Exceptions : 
 




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

Output: 
 

false

 




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

Output: 
 

true

 

Supported Browser:

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.

 




Article Tags :