JavaScript (ES2026) reference · String Methods

JavaScript String endsWith() Method

Return true if the string ends with the given text. Case-sensitive; an optional length treats the string as shorter.

JavaScript (ES2026) Since ES2015

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

string.endsWith(searchString, endPosition)

Parameters

ParameterDescription
searchString Text to look for.
endPosition optional Treat the string as this long.

Return value

true or false.

Examples

Check a file extensionTry it yourself
const file = "photo.JPG";
console.log(file.endsWith(".jpg"), file.toLowerCase().endsWith(".jpg"));

Output

false true
With an end positionTry it yourself
console.log("Hello world".endsWith("Hello", 5));

Output

true

Version history

VersionChange
JavaScript ES2015Added

In other languages

Was this page helpful?