String.prototype.endsWith()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2015.
TheendsWith() method ofString values determines whether a string ends with the characters of this string, returningtrue orfalse as appropriate.
In this article
Try it
const str1 = "Cats are the best!";console.log(str1.endsWith("best!"));// Expected output: trueconsole.log(str1.endsWith("best", 17));// Expected output: trueconst str2 = "Is this a question?";console.log(str2.endsWith("question"));// Expected output: falseSyntax
endsWith(searchString)endsWith(searchString, endPosition)Parameters
searchStringThe characters to be searched for at the end of
str. Cannotbe a regex. All values that are not regexes arecoerced to strings, so omitting it or passingundefinedcausesendsWith()to search for the string"undefined", which is rarely what you want.endPositionOptionalThe end position at which
searchStringis expected to be found (the index ofsearchString's last character plus 1). Defaults tostr.length.
Return value
true if the given characters are found at the end of the string, including whensearchString is an empty string; otherwise,false.
Exceptions
TypeErrorThrown if
searchStringis a regex.
Description
This method lets you determine whether or not a string ends with another string. This method is case-sensitive.
Examples
>Using endsWith()
const str = "To be, or not to be, that is the question.";console.log(str.endsWith("question.")); // trueconsole.log(str.endsWith("to be")); // falseconsole.log(str.endsWith("to be", 19)); // trueSpecifications
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-string.prototype.endswith> |