JavaScript endsWith()Method
Example
Determine whether a string ends with the specified substring (case-sensitive):
let str = "Hello world";
str.endsWith("world") //Returns true
str.endsWith("World") //Returns false
Try it Yourself »
Definition and Usage
The endsWith() method is used to determine whether the current string ends with the specified substring (case-sensitive).
If the passed substring is at the end of the search string, it returns true; otherwise, it returns false.
Browser Support
The numbers in the table indicate the first browser version that supports this property.
| Chrome 41 | Edge 12 | Firefox 17 | Safari 9 | Opera 36 |
| Mar 2015 | Jul 2015 | Oct 2012 | Oct 2015 | Mar 2016 |
Syntax
string.endsWith(searchvalue, length)
Parameter Values
| Parameter | Description |
|---|---|
| searchvalue | Required. The substring to search for. |
| length | Sets the length of the string. The default value is the original string length, string.length. |
Return Value
| Type | Description |
|---|---|
| Boolean | Returns true if the string ends with the specified value, otherwise returns false. |
Technical Details
| Return Value: | A Boolean. Returns true if the passed substring is at the end of the search string, otherwise returns false. |
|---|---|
| JavaScript Version: | ECMAScript 6 |
More Examples
Example
Set different string lengths to determine:
var str = "To be, or not to be, that is the question.";
str.endsWith("question."); // true
str.endsWith("to be"); // false
str.endsWith("to be", 19); // true
Try it Yourself »
Other Extensions
JavaScript String Object