String

String.prototype.endsWith

Determines whether a string ends with the characters of a specified string

Syntax

JavaScript
string.endsWith(searchString, length?)

Parameters

ParameterTypeDescription
searchStringstringThe characters to be searched for at the end
lengthnumberUsed as the length of string (defaults to string.length)

Return Value

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

Examples

Basic Usage
const str = 'Hello World';
console.log(str.endsWith('World')); // true
console.log(str.endsWith('Hello')); // false
Practical Example
const file = 'image.png';
if (file.endsWith('.png') || file.endsWith('.jpg')) {
  console.log('Image file');
}
Advanced Usage
const url = 'https://api.example.com/';
console.log(url.endsWith('/')); // true

Understanding String.prototype.endsWith

The String.prototype.endsWith method in JavaScript determines whether a string ends with the characters of a specified string. It belongs to the String object and is one of the most widely used methods for working with string values in modern JavaScript and TypeScript applications.

The method signature is string.endsWith(searchString, length?). It accepts 2 parameters: searchString, length. When called, it returns true if the string ends with the search string, false otherwise. Understanding when and how to use endsWith() helps you write more expressive, readable code.

Common use cases for String.prototype.endsWith include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like string-startswith, string-includes, string-indexof, enabling you to chain operations together for complex data manipulation pipelines.

Browser support for String.prototype.endsWith is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.

Browser Compatibility

Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.

Related Methods

More String Methods

Other methods in the String object

Related Tools

More String Methods

Explore JavaScript Methods

Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.