String.endsWith()
Checks if a string ends with a substring (true/false).
string.endsWith(searchString, length?)This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Checks if a string ends with a substring (true/false).
String.prototype.endsWith() is a case-sensitive method introduced in ES6 that determines whether a string concludes with the characters of a specified search string, returning a boolean value. For performance-critical applications, it is significantly more efficient than using regular expressions or manual substring slicing (str.slice(-length) === suffix) because it avoids creating intermediate string objects in memory. Edge cases include searching for an empty string, which always returns true, and handling the optional length parameter. When length is provided, the method treats the string as if it ended at that specific character position (effectively str.substring(0, length)). This is particularly useful for validating patterns within specific segments of a string. Note that this method is not supported in Internet Explorer and requires a polyfill for legacy environments.
Quick reference
Syntax
string.endsWith(searchString, length?)
Inputs
Parameters
See it in practice
Examples
Basic File Extension Validation
var filename = 'document_v1.pdf';
var isPdf = filename.endsWith('.pdf');
console.log(isPdf);true
A common use case for checking if a string ends with a specific file format extension.
Case Sensitivity and Normalization
var greeting = 'Hello World';
var caseSensitive = greeting.endsWith('world');
var normalized = greeting.toLowerCase().endsWith('world');
console.log('Case Sensitive:', caseSensitive);
console.log('Normalized:', normalized);Case Sensitive: false Normalized: true
Demonstrates that the method is case-sensitive and shows how to perform a case-insensitive check by normalizing the string first.
Using the Length Parameter as a Virtual End
var phrase = 'JavaScript is powerful';
// Search 'is' as if the string was only 13 characters long ('JavaScript is')
var result = phrase.endsWith('is', 13);
console.log(result);true
The second parameter limits the search range to the first N characters of the string.
Debug faster
Common Errors
TypeError
Cause: Passing a Regular Expression as the searchString argument.
Fix: Use a plain string as the argument or use RegExp.test() if pattern matching is required.
'hello'.endsWith(/lo/);LogicError
Cause: Confusing the second parameter (length/endPosition) with a start index.
Fix: Remember the second parameter acts as the 'end' boundary, not where the search starts.
// Expecting to find 'Script' starting at index 4
'JavaScript'.endsWith('Script', 4); // Returns false ('Java' does not end with 'Script')Runtime support
Compatibility
Source: MDN Web Docs
Common questions
Frequently Asked Questions
Checks if a string ends with a substring (true/false).
searchString: Suffix to check for. length: Treat string as having this length (optional).
TypeError: Use a plain string as the argument or use RegExp.test() if pattern matching is required. LogicError: Remember the second parameter acts as the 'end' boundary, not where the search starts.