JavaScript

JavaScript String endsWith() method | Explained

While programming in JavaScript, we may often encounter a situation where we have to check the ending characters of a string. For instance, you are developing a Unit converter JavaScript application, and it is required to validate the current measurement unit before proceeding further. For this purpose, ES6 introduced a JavaScript String endsWith() method that can be used to search out single or multiple characters at the end of the specified string.

This write-up will explain the usage of avaScript String endsWith() method. So, let’s start!

JavaScript String endsWith() method

In JavaScript, the “endsWith()” method is utilized to find out if the specified string ends with particular characters or not. This method returns a “boolean” value, where “true” represents that the specified substring is found at the ending of the “string”, and “false” indicates that the searched substring is not a part of it.

Syntax

string.endsWith(substring, length)

Here, the “endsWith()” method will search the “substring” within the specified length of the “string” value.

How to use JavaScript String endsWith() method

As mentioned earlier, “substring” is a required argument that we have to pass to the JavaScript String “endsWith()” method for searching purposes and it can comprise single or multiple characters. The “endsWith()” method matches that argument value with the specified string ending characters and returns “true” in case both values get matched; otherwise, the return case of the “endsWith()” method is set to “false”.

Have a look at the below-given examples to understand the working of the String “endsWith()” method.

Example: Searching single character
First of all, we will create a constant named “string” having the following value:

const string= 'linuxhint';

With the help of the “endsWith()” method, we will now check if the value of the “string” ends with the character “l”:

string.endsWith('l');

Execution of the above-given command will return “false” because the last character of the “string” value is “t,” not “l”:

Example: Searching multiple characters
Similarly, using the String “endsWith()” method, you can validate if a string comprises a “substring” or the passed “multiple characters” at its end or not.

For instance, the following “endsWith()” method will check if the “string” value contains “hint” as its ending characters:

string.endsWith('hint');

In this case, the “string.endsWith()” method will return “true” because the ending characters of the “linuxhint” matched with the added argument value:

Example: Searching characters with length
The JavaScript String “endsWith()” method also permits you to search characters within a specific “length”. For this purpose, you have to pass two arguments to the “endsWith()” method. The first argument refers to the substring that needs to be matched, and the second argument indicates the number of characters or the length within which the search operation will be performed.

Before executing the “endsWith()” method, we will check out the length of the “linuxhint” property by utilizing the String “length” property:

console.log('linuxhint'.length);

The given output signifies that the string “linuxhint” has “9” characters:

In the next step, we will create another “string” and initialize it with the value “linuxhint website”:

const string= 'linuxhint website';

Then, we will invoke the “endsWith()” method for the created “string” while passing “nt” as “substring” and “9” as “length”:

string.endsWith('nt', 9);

When the given “string.endsWith()” method executes, it will grab the first “9” characters of the string “linuxhint website” and then search “nt” substring in its ending characters. This operation will return the “true” value as the “linuxhint” string ends with “nt”:

Example: Case-Sensitive Searching
Another important point to remember is that the JavaScript String “endsWith()” method is “case-sensitive“. So, you have to take care of the searched “substring” characters case.

For instance, the below-given “endsWith()” will perform case-sensitive searching in the “string” value:

string.endsWith('website');

As the ending characters of the “string” value and the searched substring “website” are in the same case, the “string.endsWith()” method will return “true” value:

While for the same substring having upper-case characters “WEBSITE”, the “string.endsWith()” will set its return case as “false”:

string.endsWith('WEBSITE');

Output

That was essential information related to the JavaScript String endsWith() method. You can further research it according to your preferences.

Conclusion

In JavaScript, the “endsWith()” method is utilized to find out if the specified string ends with particular characters or not. This method returns a boolean value, where true represents that the added substring is found at the string’s ending, and false indicates that the searched substring is not a part of it. This write-up explained the usage of the JavaScript String endsWith() method.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.