String endsWith() Method - Javascript String

Javascript examples for String:endsWith

Description

The endsWith() method returns true if the string ends with the characters, and false if not.

Parameter Values

Parameter Description
searchvalue Required. The string to search for
length Optional. Specify the length of the string to search. If omitted, the default value is the length of the string

Return Value:

A Boolean. Returns true if the string ends with the value, otherwise it returns false

The following code shows how to Check if a string ends with another word:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {/*ww  w.j  a va 2  s.c  o  m*/
    var str = "Hello world test world";
    var n = str.endsWith("world", 11);
    document.getElementById("demo").innerHTML = n;
}
</script>

</body>
</html>

Related Tutorials