String.prototype.endsWith()

text.endsWith(searchString, length)

Returns true when the string ends with the searched string. The optional second parameter sets how many characters of the string are taken into account.

Parameters

  • searchString – the string it should end with.
  • length – optional, only the first length characters are considered (the whole length by default).

Return value

true or false.

Examples

const file = 'Photo.JPG';
console.log(file.endsWith('.jpg'));
console.log(file.toLowerCase().endsWith('.jpg'));
console.log(['.jpg', '.png', '.webp'].some((e) => file.toLowerCase().endsWith(e)));
console.log('index.php?x=1'.endsWith('.php', 9));

Output:

false
true
true
true

Things to watch out for

  • It is case-sensitive. To search regardless of case, compare converted strings: text.toLowerCase().includes(searchString.toLowerCase()).
  • A file extension from a user guarantees nothing – the file type must be checked on the server.

Browser support

Baseline – widely available: in all major browsers since September 2015, considered widely available since March 2018. Since version: Chrome 41, Firefox 17, Safari 9; Node.js 4.0.0. As of October 2026 (MDN browser-compat-data).

Related:
String.prototype.startsWith()
String.prototype.includes()
String.prototype.lastIndexOf()