Favorites

JavaScript String.prototype.endsWith() Method

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
String.prototype.endsWith()
JavaScript String Instance methodThis method is supported by all modern browsers.
Updated 28th November 2026

The endsWith() method checks whether a string ends with given text, returning true or false. It is case-sensitive.

Description

The endsWith() method determines whether a string ends with the characters of another string. It's case-sensitive and a clear way to check things like file extensions.

The optional second argument treats the string as if it were only that many characters long, which lets you check whether a part of the string ends with something without slicing it first. An empty search string always returns true.

Passing a regular expression throws a TypeError; to test the end of a string against a pattern, use a regex with the $ anchor, as in /\.(jpe?g|png)$/i.test(file), which also handles several extensions and different letter cases at once.

Syntax

string.endsWith(searchString)
string.endsWith(searchString, endPosition)

Parameters

  • searchStringThe text to look for at the end.
  • endPositionOptional. Treat the string as if it were this long.

Return Value

true if the string ends with the search text, otherwise false.

Example

const file = 'photo.JPG';
console.log(file.endsWith('.jpg'));
console.log(file.toLowerCase().endsWith('.jpg'));
console.log('Hello world'.endsWith('Hello', 5));

Browser Support

The following information will show you the current browser support for the String.prototype.endsWith() method. Hover over a browser icon to see the version that first introduced support for this method.

This method is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee