Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
The endsWith() method in JavaScript checks whether a string ends with a specific substring and returns true or false accordingly. It is case-sensitive and allows an optional position parameter to define the search length. This method is useful for validating file extensions, URLs, and specific text patterns.
In this article, you will learn the syntax, parameters, and practical examples of the endsWith() method in JavaScript.
Description
The `endsWith()` method in JavaScript is used to determine whether a string ends with a specific set of characters. It returns `true` if the string ends with the specified characters & `false` if it doesn’t. This method is case-sensitive, which means it distinguishes between uppercase & lowercase letters.
Let’s understand with an example. Suppose you have a string:
let fileName = "document.pdf";
You want to check if this file is a PDF. Using `endsWith()`, you can easily do this:
let isPDF = fileName.endsWith(".pdf");
console.log(isPDF);
You can also try this code with Online Javascript Compiler
In this example, `endsWith()` checks if the string `fileName` ends with `.pdf`. Since it does, the method returns `true`.
Syntax
The syntax for the endsWith() method is as follows:
string.endsWith(searchString, length)
string: The original string to be checked.
searchString: The characters to check at the end of the string.
length (optional): Specifies the length of the string to consider. If omitted, the entire string length is used.
Parameters
searchString(Required): The sequence of characters to check if they match the end of the string.
length(Optional): The length of the string to consider. If specified, endsWith() checks whether the substring (from index 0 to length) ends with searchString.
Return Value
The endsWith() method returns:
true if the string ends with the specified searchString.
false if the string does not end with searchString.
Examples
Example 1: Basic Usage
let text = "Hello, World!";
console.log(text.endsWith("World!"));
console.log(text.endsWith("Hello"));
You can also try this code with Online Javascript Compiler
Here, the length parameter specifies that only the first 13 characters ("JavaScript is") should be considered. Since "is" appears at the end of this substring, the method returns true.
Example 3: Checking File Extensions
let fileName = "document.pdf";
if (fileName.endsWith(".pdf")) {
console.log("This is a PDF file.");
} else {
console.log("This is not a PDF file.");
}
You can also try this code with Online Javascript Compiler
The method is case-sensitive, so "world" and "World" are treated as different strings.
Practical Use Cases
1. File Type Validation: You can use `endsWith()` to check if a file has a specific extension, like `.jpg`, `.png`, or `.pdf`.
let imageFile = "photo.jpg";
if (imageFile.endsWith(".jpg")) {
console.log("This is a JPEG image.");
} else {
console.log("This is not a JPEG image.");
}
2. Input Validation: If you’re building a form & want to ensure an email ends with `@gmail.com`, you can use `endsWith()`.
let userEmail = "example@gmail.com";
if (userEmail.endsWith("@gmail.com")) {
console.log("Valid Gmail address.");
} else {
console.log("Invalid Gmail address.");
}
3. Filtering Data: Suppose you have a list of URLs & want to filter out only those that end with `.org`.
let urls = ["https://example.com", "https://example.org", "https://example.net"];
let orgUrls = urls.filter(url => url.endsWith(".org"));
console.log(orgUrls);
You can also try this code with Online Javascript Compiler
In the first case, `"world!"` is in lowercase, so the method returns `false`. In the second case, `"World!"` matches exactly, so it returns `true`.
2. Handling Empty Strings
If you pass an empty string (`""`) as the `searchString`, `endsWith()` will always return `true`. This is because every string technically ends with an empty string.
let text = "JavaScript";
console.log(text.endsWith(""));
You can also try this code with Online Javascript Compiler
Here, the number `45` is converted to the string `"45"`, & the method checks if the string ends with `"45"`.
5. Browser Compatibility
`endsWith()` is supported in all modern browsers, including Chrome, Firefox, Safari, & Edge. However, it is not supported in Internet Explorer. If you need to support older browsers, you might need to use a polyfill or an alternative approach.
Example: Polyfill for Older Browsers
If you’re working in an environment where `endsWith()` might not be available, you can create a polyfill:
if (!String.prototype.endsWith) {
String.prototype.endsWith = function(searchString, length) {
if (length === undefined || length > this.length) {
length = this.length;
}
return this.substring(length - searchString.length, length) === searchString;
};
}
let text = "Hello, world!";
console.log(text.endsWith("world!"));
You can also try this code with Online Javascript Compiler
What happens if the searchString is longer than the original string?
If searchString is longer than the given string, the method will return false because a string cannot end with more characters than it contains.
Does endsWith() work with numbers?
No, endsWith() is designed for strings. If you want to use it with numbers, convert the number to a string before calling endsWith().
How is endsWith() different from includes()?
endsWith() checks if a string ends with specific characters, whereas includes() checks if a string contains specific characters anywhere within it.
Conclusion
In this article, we explored the endsWith() method in JavaScript, which checks if a string ends with a specified substring. It returns true if the string matches the given ending and false otherwise. This method is useful for validating inputs, filtering data, and handling text-based conditions. Understanding endsWith() method in JavaScript helps in writing cleaner and more efficient code.
Live masterclass
Microsoft SDE Roadmap: Use AI Tools to Succeed
by Pranav Malik
19 May, 2025
01:30 PM
Break into MAANG Data Analyst roles from Non-Tech Profession
by Abhishek Soni
20 May, 2025
01:30 PM
SDE LinkedIn & Naukri Hacks to Get More Recruiter Calls
by Shantanu Shubham
21 May, 2025
01:30 PM
Amazon Data Analyst: Advanced Excel & AI Interview Tips
by Megna Roy
22 May, 2025
01:30 PM
Microsoft SDE Roadmap: Use AI Tools to Succeed
by Pranav Malik
19 May, 2025
01:30 PM
Break into MAANG Data Analyst roles from Non-Tech Profession