Home >>JavaScript String Methods >JavaScript String includes() Method

JavaScript String includes() Method

What is JavaScript String includes() Method

JavaScript includes() method is used to find whether a string contains the characters of a defined string or not. It is case sensitive. It returns true value if the string contains the characters and false value if not.

Syntax:

string.includes(searchvalue, start)

Parameter Values

Parameter Description
searchvalue This is a required parameter. It defines the string to search for.
start This is an optional parameter. It defines at which position to start the search.

Browser Support

Method Chrome Edge Firefox Safari Opera
includes() 41 12.0 40 9 28

Here is an example of includes() method:

<html>
<body>
<p>Click the button to see the Output</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
  var str = "I am the king of this World.";
  var n = str.includes("king");
  document.getElementById("demo").innerHTML = n;
}
</script>
</body>
</html>
Output:

Click the button to see the Output

 

Example 2:

<html>
<body>
<p>Click the button to see the Output</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
  var str = "I am the king of this World.";
  var n = str.includes("king", 9);
  document.getElementById("demo").innerHTML = n;
}
</script>
</body>
</html>
Output:

 

Click the button to see the Output