Table of contents
1.
Introduction
2.
Description  
3.
Syntax
4.
Parameters
5.
Return Value
6.
Examples
6.1.
Example 1: Basic Usage
6.2.
Example 2: Using the length Parameter
6.3.
Example 3: Checking File Extensions
6.4.
Example 4: Case Sensitivity
7.
Practical Use Cases  
7.1.
Specifications  
8.
Supported Browsers
9.
Frequently Asked Questions
9.1.
What happens if the searchString is longer than the original string?
9.2.
Does endsWith() work with numbers?
9.3.
How is endsWith() different from includes()?
10.
Conclusion
Last Updated: Feb 18, 2025
Medium

What is endsWith() Method in JavaScript

Author Pallavi singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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.

What is endsWith() Method in JavaScript

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
Run Code


Output: 

true

 

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

  1. searchString (Required): The sequence of characters to check if they match the end of the string.
     
  2. 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
Run Code

 

Output: 

true
false

Example 2: Using the length Parameter

let sentence = "JavaScript is powerful";
console.log(sentence.endsWith("is", 13)); 
console.log(sentence.endsWith("Java"));   
You can also try this code with Online Javascript Compiler
Run Code

 

Output: 

true
false

 

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
Run Code

 

Output:

This is a PDF file.

Example 4: Case Sensitivity

let message = "HelloWorld";
console.log(message.endsWith("world")); 
console.log(message.endsWith("World")); 
You can also try this code with Online Javascript Compiler
Run Code


Output: 

false
true

 

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
Run Code

 

Output:

 ["https://example.org"]

Specifications  

The `endsWith()` method has some important specifications that you should know to use it effectively. Let’s discuss them one by one.  

1. Case Sensitivity  

`endsWith()` is case-sensitive. This means it treats uppercase & lowercase letters as different characters. For example:  

let text = "Hello, World!";
console.log(text.endsWith("world!")); 
console.log(text.endsWith("World!")); 
You can also try this code with Online Javascript Compiler
Run Code

 

Output:

false 
true


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
Run Code

 

Output: 

true

 

3. Optional Length Parameter  

The `length` parameter allows you to specify how much of the string to consider. If you don’t provide it, the method checks the entire string.  

let text = "Programming is fun!";
console.log(text.endsWith("fun", 17)); 
You can also try this code with Online Javascript Compiler
Run Code

 

Output: 

true

 

Here, the method only checks the first 17 characters of the string. Since `"fun"` is at the end of these 17 characters, it returns `true`.  

 

4. Non-String Inputs  

If you pass a non-string value as the `searchString`,JavaScript will automatically convert it to a string. For example:  

let text = "12345";
console.log(text.endsWith(45)); 
You can also try this code with Online Javascript Compiler
Run Code

 

Output: 

true

 

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
Run Code

 

 Output: 

true

 

This code checks if the `endsWith()` method is already defined. If not, it adds the method to the `String` prototype.  

Supported Browsers

The endsWith() method in JavaScript is supported in most modern browsers, including:

BrowserSupported Version
Chrome41+
Firefox17+
Edge12+
Safari9+
Opera28+
Internet Explorer (IE)Not Supported


For Internet Explorer support, you can use a polyfill:

if (!String.prototype.endsWith) {
    String.prototype.endsWith = function(search, length) {
        let str = this.toString();
        if (length === undefined || length > str.length) {
            length = str.length;
        }
        return str.substring(length - search.length, length) === search;
    };
}

Frequently Asked Questions

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