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

JavaScript String endsWith() Method

JavaScript String endsWith() Method

JavaScript endsWith() method is used to find if a given input string ends with the characters of a specified string or not. It is case sensitive. It returns true value if the string ends with the characters and false value if not.

Syntax:
string.endsWith(searchvalue, length);

Parameter Values

Parameter Description
searchvalue This is a required parameter. It defines the string to search for.
length This is an optional parameter. It defines the length of the string to search.

Browser Support

Method Chrome Edge Firefox Safari Opera
endsWith() 41 12.0 17 9 36
Here is an example of endWith() 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.endsWith("World.");
  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.endsWith("king", 13);
  document.getElementById("demo").innerHTML = n;
}
</script>
</body>
</html>
Output:

Click the button to see the Output.