Developed By
Gautam Kumar - Full stack developer
DEEP DIVE INTO
The endsWith()
method in JavaScript is a built-in method used to determine whether a string ends with a specified substring. It returns a boolean value (true or false) based on whether the string ends with the given substring. Let's take a deep dive into the endsWith()
method and explore how it works.
The basic syntax of the endsWith()
method is as follows:
javascriptstring.endsWith(searchString[, length])
string: The string on which you want to perform the check.
searchString: The substring
you want to check if the string ends with.
length (optional): An integer representing the length of the string to consider. If omitted, the entire string is considered.
javascriptconst text = "Hello, world!";
const endsWithWorld = text.endsWith("world!");
console.log(endsWithWorld); // Output: true
In this example, we use the endsWith()
method to check if the text string ends with the substring "world!". Since it does, the method returns true.
The length parameter allows you to specify how many characters from the end of the string to consider when checking. This can be useful if you want to check for the presence of a substring within a specific portion of the string.
javascriptconst text = "Hello, world!";
const endsWithHello = text.endsWith("Hello", 5);
console.log(endsWithHello); // Output: true
In this example, we specify length as 5, indicating that we want to consider only the first 5 characters from the end of the string. As a result, the method returns true because "Hello" matches the specified portion of the string.
By default, the endsWith()
method performs a case-sensitive comparison. This means that the substring
you provide must match the case of the characters at the end of the string for it to return true.
javascriptconst text = "Hello, world!";
const endsWithWorld = text.endsWith("World!");
console.log(endsWithWorld); // Output: false
In this example, the endsWith()
method returns false because "World!" does not match "world!" in terms of case.
To perform a case-insensitive comparison, you can convert both the string and the substring
to lowercase or uppercase before using the endsWith()
method.
javascriptconst text = "Hello, world!";
const endsWithWorldIgnoreCase = text.toLowerCase().endsWith("World!".toLowerCase());
console.log(endsWithWorldIgnoreCase); // Output: true
In this example, we convert both the text string and the search substring
"World!" to lowercase before applying the endsWith()
method. This ensures a case-insensitive comparison and returns true.
The endsWith()
method is commonly used for various purposes, including:
1. File extension checking: Verifying if a file path ends with a particular extension (e.g., ".jpg", ".pdf").
2. URL validation: Checking if a URL ends with a specific path or file name.
3. Token validation: Verifying if a token or code ends with a specific delimiter or pattern.
4. Text filtering: Filtering strings based on their endings.
5. String manipulation: Creating conditional logic based on the end of a string.
In summary, the endsWith()
method in JavaScript is a straightforward and useful tool for checking if a string ends with a specific substring. It's widely used for conditional logic, validation, and text processing in JavaScript applications.