Substring check in JavaScript
The includes() method can be used to check whether a string contains a specified substring. It returns true if substring is present. This method is case sensitive.
Syntax:
string.includes(searchvalue, start)
Parameters: This method requires 2 parameters :-
- searchvalue: Required. It specifies the substring that needs to be searched.
- start: Optional. It specifies the index from where the searching is performed. By default it is 0.
Return Value: True if the specified substring is present in string else returns false.
Example:
<!DOCTYPE html> <html> <body> <h1>Welcome to GeeksforGeeks</h1> <p>Click the button to check whether <b>Geeks</b> is present in the below string. </p> Hello there! Welcome to GeeksforGeeks.<br><br> <button onclick="myFunction()">Try it</button> <p id="display"></p> <!--script to check string contains the specified substring.--> <script> function myFunction() { var str = "Hello there! Welcome to GeeksforGeeks."; var flag = str.includes("Geeks"); document.getElementById("display").innerHTML = flag; } </script> </body> </html> |
Output:
Before clicking try it button:

After clicking try it button:

Supported Browsers: The browser supported by includes() Method are listed below:
- Google Chrome 41
- Apple Safari 9
- Firefox 40
- Opera 28
- Edge 12.0
Recommended Posts:
- How to check a string is entirely made up of the same substring in JavaScript ?
- JavaScript | string.substring()
- Difference between substr() and substring() in JavaScript
- JavaScript | Difference between String.slice and String.substring
- How to check if a string is html or not using JavaScript?
- How to check two elements are same using jQuery/JavaScript ?
- JavaScript | Check the existence of variable
- Check a number is Prime or not using JavaScript
- How to check a function is defined in JavaScript ?
- How to check if an element has any children in JavaScript ?
- JavaScript | Check if a variable is a string
- How to Check/Uncheck the checkbox using JavaScript ?
- How to check a key exists in JavaScript object?
- How to check a date is valid or not using JavaScript ?
- How to check object is an array in JavaScript?
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.



