String.prototype.endsWith()
BaselineWidely 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.
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: false
Syntax
endsWith(searchString)endsWith(searchString, endPosition)
Parameters
searchString
The 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 passingundefined
causesendsWith()
to search for the string"undefined"
, which is rarely what you want.endPosition
OptionalThe end position at which
searchString
is 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
TypeError
Thrown if
searchString
is 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)); // true
Specifications
Specification |
---|
ECMAScript® 2026 Language Specification # sec-string.prototype.endswith |