Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

String.prototype.endsWith()

BaselineWidely available

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

js
endsWith(searchString)endsWith(searchString, endPosition)

Parameters

searchString

The characters to be searched for at the end ofstr. 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.

endPositionOptional

The end position at whichsearchString 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 ifsearchStringis 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()

js
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

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp