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
Syntax
string.endsWith(searchString, endPosition)
Parameters
| Parameter | Description |
|---|---|
searchString |
Text to look for. |
endPosition optional |
Treat the string as this long. |
Return value
true or false.
Examples
const file = "photo.JPG";
console.log(file.endsWith(".jpg"), file.toLowerCase().endsWith(".jpg"));
Output
false true
console.log("Hello world".endsWith("Hello", 5));
Output
true
Version history
| Version | Change |
|---|---|
| JavaScript ES2015 | Added |
In other languages
str.endswith()· Pythonstr_ends_with()· PHP
Related
string.startsWith()· Return true if the string begins with the given text. Case-sensitive; an optional position starts the check later.string.includes()· Return true if the string contains the given text anywhere. Case-sensitive.- JavaScript Strings · tutorial
- JavaScript String Methods · tutorial