JavaScript String.prototype.endsWith() Method
The endsWith() method checks whether a string ends with given text, returning true or false. It is case-sensitive.
Description
The endsWith() method determines whether a string ends with the characters of another string. It's case-sensitive and a clear way to check things like file extensions.
The optional second argument treats the string as if it were only that many characters long, which lets you check whether a part of the string ends with something without slicing it first. An empty search string always returns true.
Passing a regular expression throws a TypeError; to test the end of a string against a pattern, use a regex with the $ anchor, as in /\.(jpe?g|png)$/i.test(file), which also handles several extensions and different letter cases at once.
Syntax
string.endsWith(searchString)
string.endsWith(searchString, endPosition)Parameters
- searchStringThe text to look for at the end.
- endPositionOptional. Treat the string as if it were this long.
Return Value
true if the string ends with the search text, otherwise false.
Example
Browser Support
The following information will show you the current browser support for the String.prototype.endsWith() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





