Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

String.prototype.endsWith

Checks whether a string ends with the given text.

Available since: ES2015

Syntax

str.endsWith(searchString, endPosition)

Parameters

ParameterTypeDescription
searchStringstringThe text the string should end with.
endPositionnumberOptional. Treat the string as if it had this length. The whole string by default.

Return value

booleantrue if the string ends with that text.

Examples

Checking a file extension:

console.log('photo.jpg'.endsWith('.jpg'));
console.log('photo.png'.endsWith('.jpg'));

Output:

true
false

Case matters — convert the string to one case first:

const file = 'PHOTO.JPG';
 
console.log(file.endsWith('.jpg'));
console.log(file.toLowerCase().endsWith('.jpg'));

Output:

false
true

Notes

Its pair is startsWith(). Neither method accepts regular expressions.

See also

Related recipes

In the tutorial

  • String methods — Useful string methods in JavaScript: finding a position, reading characters, padStart, repeat, split and join, method chains.

Thanks for reading!

Donate to the site — we work hard for you.