String.prototype.endsWith()

endsWith(searchString, endPosition)
Returns: boolean · Updated March 16, 2026 · String Methods
string endswith searching

The endsWith() method determines whether a string ends with the characters of a specified string, returning true or false as appropriate. It’s the counterpart to startsWith() and provides a cleaner alternative to checking the last N characters of a string.

Syntax

str.endsWith(searchString)
str.endsWith(searchString, endPosition)

Parameters

ParameterTypeDefaultDescription
searchStringstringThe characters to search for at the end of the string.
endPositionnumberstr.lengthThe end position within the string to search. If omitted, defaults to the string’s length.

Examples

Basic usage

const str = 'Hello, world!';

console.log(str.endsWith('world!'));
// true

console.log(str.endsWith('Hello'));
// false

console.log(str.endsWith('world!', 13));
// true

Case-sensitive matching

const filename = 'document.pdf';

console.log(filename.endsWith('.pdf'));
// true

console.log(filename.endsWith('.PDF'));
// false (case-sensitive)

console.log(filename.endsWith('document'));
// false

Using with file extensions

const files = [
  'image.png',
  'photo.jpg',
  'document.pdf',
  'archive.zip'
];

const images = files.filter(file => file.endsWith('.png') || file.endsWith('.jpg'));
console.log(images);
// ['image.png', 'photo.jpg']

Common Patterns

Validating file types

function isImage(filename) {
  const imageExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.webp'];
  return imageExtensions.some(ext => filename.toLowerCase().endsWith(ext));
}

console.log(isImage('photo.PNG'));
// true

console.log(isImage('document.pdf'));
// false

Checking URL endings

function isSecureUrl(url) {
  return url.endsWith('https');
}

console.log(isSecureUrl('https://example.com'));
// true

console.log(isSecureUrl('http://example.com'));
// false

Use Cases

URL validation

When building web applications, you often need to validate that URLs end with certain patterns. The endsWith() method makes this straightforward:

function hasTrailingSlash(url) {
  return url.endsWith('/');
}

console.log(hasTrailingSlash('https://example.com/page/'));
// true

console.log(hasTrailingSlash('https://example.com/page'));
// false

Extension checking in upload forms

In file upload scenarios, validating the file extension on the client side provides immediate feedback:

function validateUpload(filename, allowedExtensions) {
  return allowedExtensions.some(ext => 
    filename.toLowerCase().endsWith(ext.toLowerCase())
  );
}

const documentExtensions = ['.pdf', '.doc', '.docx'];
console.log(validateUpload('resume.PDF', documentExtensions));
// true

Comparing string endings

Unlike manual comparisons using slice() or substr(), endsWith() is semantic and readable:

const message = 'Hello, JavaScript developer!';

// Using endsWith() - clear intent
console.log(message.endsWith('developer!'));
// true

// Manual approach - harder to read
console.log(message.slice(-10) === 'developer!');
// true (but less clear)

Performance Considerations

The endsWith() method is optimized in modern JavaScript engines. For older environments, a polyfill is available. The method handles Unicode correctly and doesn’t suffer from the edge cases that plague manual length calculations.

Edge Cases

// Empty search string always returns true
console.log('hello'.endsWith(''));
// true

// Position beyond string length uses string length
console.log('hi'.endsWith('hi', 100));
// true

// Case matters
console.log('HELLO'.endsWith('hello'));
// false

See Also