String.prototype.endsWith()
Determines whether a string ends with the given substring.
Syntax
str.endsWith(searchString, endPosition) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
searchString | string | Yes | The substring to test for at the end. |
endPosition | number | No | Treat the string as if it were only this many characters long. Defaults to string length. |
Returns
boolean — true if the string ends with the substring; otherwise false.
Examples
console.log('image.png'.endsWith('.png'));
Output
true
console.log('hello world'.endsWith('hello', 5));
Output
true
Notes
Case-sensitive. Throws TypeError if given a regular expression. The
endPosition argument effectively checks a prefix of the string.
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | 41 |
| firefox | 17 |
| safari | 9 |
| edge | 12 |
| node | 4.0 |