/

HTML / CSS / JavaScript Tutorial

JavaScript String method: endsWith()

[this page | pdf | back links]

The endsWith() method (when applied to a JavaScript string) returns true if the string ends with a specified string, otherwise returns false.

 

It has the following syntax with the following parameters:

 

string.endsWith(searchstring, length)

 

Parameter

Required / Optional

Description

searchstring

Required

String to be searched for

length

Optional

(Default is string.length). Length of string to search

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
table,th,tr,td {border: 1px solid black; border-collapse: collapse;}
</style>
</head>
<body>
<table>
<tr>
<th>Example</th>
<th>Resulting value of <code>x</code></th>
</tr>
<tr>
<td><code id="Example"></code></td>
<td><code id="Result"></code></td>
</tr>
</table>

<script>
var y = "abcde", y2 = "cde";
document.getElementById("Example").innerHTML =
  'var y = "' + y + '";<br>' +
  'var x = y.endsWith("' + y2 + '");';
document.getElementById("Result").innerHTML
  = y.endsWith(y2);
</script>

</body>
</html>

FUNCTION THAT MAY ASSIST IN TESTING WHETHER FEATURE IS SUPPORTED:
function isSupportedJavaScriptMethodStringEndsWith() {
  var z = "abc"; return !!z.endsWith;
}


NAVIGATION LINKS
Contents | Prev | Next | JavaScript String Variables


Desktop view | Switch to Mobile