endsWith()

ES6+

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

Syntax

string.endsWith(searchString, length)

Parameters

searchString string

The characters to search for at the end

length number optional

Length of string to search within

Return Value

boolean

true if the string ends with the given characters

Examples

JavaScript
const str = 'Hello World';
console.log(str.endsWith('World'));
console.log(str.endsWith('Hello'));
Output:
// true false

📌 When to Use

Use endsWith() to check file extensions, validate URL suffixes, verify string format endings, or any situation where you need to confirm what a string ends with.

⚠️ Common Mistakes

Using complex regex or lastIndexOf for simple suffix checks when endsWith() is cleaner and more efficient.

Forgetting that file extensions are case-sensitive in endsWith() - ".JPG" !== ".jpg" on case-sensitive systems.

Not understanding the length parameter - it acts as the virtual end position, not a count of characters to check.

✅ Best Practices

Always use toLowerCase() when checking file extensions to handle variations like .JPG, .Jpg, .jpg.

Use endsWith() with an array of extensions and some() for validating multiple file types.

Combine with slice() or substring() to extract the suffix after validation: str.slice(-ext.length).

⚡ Performance Notes

endsWith() is optimized to only compare the necessary characters at the end of the string. It is faster than using slice(-n) === suffix or regex patterns for suffix matching, especially for long strings.

🌍 Real World Example

File Extension and Type Validation

endsWith() is widely used for file upload validation, filtering files by type, and ensuring URLs have correct extensions or suffixes.

// Validate file extension for upload
function isAllowedImageFile(filename) {
  const allowedExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.webp'];
  const lowerName = filename.toLowerCase();
  return allowedExtensions.some(ext => lowerName.endsWith(ext));
}

console.log(isAllowedImageFile('photo.JPG')); // true
console.log(isAllowedImageFile('document.pdf')); // false

// Ensure URL ends with trailing slash for consistency
function normalizeUrl(url) {
  if (!url.endsWith('/')) {
    return url + '/';
  }
  return url;
}

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

// Get file type category from filename
function getFileCategory(filename) {
  const lower = filename.toLowerCase();

  if (['.jpg', '.jpeg', '.png', '.gif', '.webp'].some(e => lower.endsWith(e))) {
    return 'image';
  }
  if (['.mp4', '.webm', '.avi', '.mov'].some(e => lower.endsWith(e))) {
    return 'video';
  }
  if (['.mp3', '.wav', '.ogg', '.flac'].some(e => lower.endsWith(e))) {
    return 'audio';
  }
  if (['.pdf', '.doc', '.docx', '.txt'].some(e => lower.endsWith(e))) {
    return 'document';
  }
  return 'other';
}

console.log(getFileCategory('report.PDF')); // 'document'
console.log(getFileCategory('song.mp3')); // 'audio'

Related Methods