search

String.prototype.endsWith

Returns true if the string ends with searchString when considered up to endPosition (default str.length). Useful for file extension and suffix checks.

str.endsWith(searchString[, endPosition])

Parameters

Parameter Purpose
searchString Suffix to test for
endPosition Treat the string as if it were only this long (default str.length)

Examples

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

Prints true

console.log('hello world'.endsWith('hello', 5));

Prints true

console.log('README.MD'.endsWith('.md'));

Prints false (case-sensitive)

console.log('abc'.endsWith(''));

Prints true

Gotcha

endPosition trims the effective string length rather than being the last index to match at. RegExp arguments throw TypeError.

Related methods

← All JS string methods · JS array methods