JavaScript String includes a method used to determines whether a searching string contains found within another string. It will return a true if the string contains the characters otherwise false.
Note: The JS includes() method is case sensitive
Syntax
string.includes(searchvalue, start)
Parameter Values
- searchvalue: The string to search for.
- start: At which position to start the search, optional and the default value is 0.
Return Value:
It will return true if string included else false indicating the absence.
Example of JavaScript String includes method
Let’ see an example of the string contains a character or not in JS using the includes() method.
Let’s see if a string includes “world”:
<!DOCTYPE html>
<html>
<head>
<script>
var str = "Hello world, welcome to the EyeHunts.com";
var n = str.includes("world");
alert(n)
</script>
</head>
</html>
Output:
Same example with starting the search at position 10:
It will return false because “world” word will not found after 10 portion.
<!DOCTYPE html>
<html>
<head>
<script>
var str = "Hello world, welcome to the EyeHunts.com";
var n = str.includes("world", 10);
alert(n)
</script>
</head>
</html>
Output:
Do comment about this tutorial. If any suggestions and example then do comment.
Note: The All JS Examples codes are tested on the Safari browser (Version 12.0.2) and Chrome.
OS: macOS 10.14 Mojave
Code: HTML 5 Version