JavaScript String endsWith() - Check String Ending

Updated on November 13, 2024
endsWith() header image

Introduction

The endsWith() method in JavaScript is a straightforward and efficient way to determine whether a string ends with specific characters or a substring. This method is especially useful in scenarios where you need to validate or assess string data based on its ending pattern, such as file extensions, certain protocols, suffixes, and more.

In this article, you will learn how to use the endsWith() method in various contexts. Explore different examples to understand how this method operates with direct strings, variables, and its utilization with conditional statements for effective string analysis and validation.

Using endsWith() to Check String Endings

Basic Usage of endsWith()

  1. Start with a basic string.

  2. Use the endsWith() method to check if the string ends with a specified substring.

    javascript
    let message = "Hello, world!";
    let checkEnding = message.endsWith("world!");
    console.log(checkEnding);
    

    In this code, checkEnding will be true because the string in message ends with "world!".

Case Sensitivity

  1. Remember that endsWith() is case-sensitive.

  2. Try checking the ending with a different case to illustrate sensitivity.

    javascript
    let greeting = "Hello, World!";
    let result = greeting.endsWith("world!");
    console.log(result);
    

    This code outputs false because "World!" is not the same as "world!" due to different cases in the substring.

Using Variables for Substring

  1. Use a variable to specify the substring for flexibility and dynamic checks.

  2. Implement endsWith() with a variable holding the substring.

    javascript
    let fileName = "example.pdf";
    let extension = "pdf";
    let isValidExtension = fileName.endsWith(extension);
    console.log(isValidExtension);
    

    Here, isValidExtension will return true, demonstrating how variables can be used to check the end of strings dynamically.

Combining with Conditional Logic

  1. Incorporate endsWith() within if-else structures for enhanced decision-making.

  2. Set up conditions based on the ending of a string.

    javascript
    let url = "https://example.com/data.json";
    if (url.endsWith(".json")) {
        console.log("The URL points to a JSON file.");
    } else {
        console.log("The URL does not point to a JSON file.");
    }
    

    This control flow checks if url ends with ".json"; if true, it confirms that the URL points to a JSON file.

Conclusion

The endsWith() method in JavaScript is essential for validating the ending segment of strings in various applications. Whether checking file extensions, managing URL formats, or ensuring consistency in user inputs, endsWith() enhances the robustness and reliability of string handling in JavaScript applications. Equip your toolkit with this method to streamline tasks involving string analysis and ensure data integrity through simple yet effective validation checks.