Introduction to the String endsWith method
At the base of it, the endsWith method returns true if the string ends with a given string, and false if it doesn’t. It’s as simple as that.
We could be searching for a simple character, like below:
let str = 'My string'; let bool = str.endsWith('g'); console.log(bool); //true
Since the string ends with the character ‘g’, we got a true. If we searched for ‘n’ for example, we’ll end up with a false, like shown below:
let bool = str.endsWith('n'); console.log(bool); //false
Alternatively, you can search for an entire word, and as long as it’s the last (or only) word in your string, you’ll get back a ‘true’, as shown below:
let bool = str.endsWith('string'); console.log(bool); //true
You can also search for an entire substring with multiple words, separated by commas, as shown below:
let bool = str.endsWith('My string'); console.log(bool); //true
In the above example, ‘My string’ is the entire string we’re performing our search on, but technically, it is the end of the string as well, so our program returned a ‘true’.
By default, if we give just one argument, that is, the argument that pertains to the substring we’re searching for, the program will search the entire length of the string to find if it ends with the given substring.
But we can shorten our search to a particular piece of the string as well. Let’s look at how to do that next.
Specify the search position
Unlike with the startsWith method, the second argument in the endsWith method is not for the position from which we need to start the search, but for the length of the string whose ending we need to look out for.
If we give the value as 15, when the string’s length is 30, then it’ll only look at the end of the first half of the string.
Let’s look at an example to understand this concept better.
Now, let’s say we have a string ‘This is my string’. It has 17 characters in it.
Now, if I use the endsWith method to see if the string ends with the substring ‘my’, I’ll get back a ‘false’ as shown below:
let str = 'This is my string'; let bool = str.endsWith('my'); console.log(bool); //false
That’s fair. Our entire string doesn’t end with ‘my’, after all.
But what if I only want to check the first 10 characters of the string, meaning the first 3 words in the string, which would be ‘This is my’. Now, the end of this string is indeed ‘my’, isn’t it? I should get a ‘true’ value if I use the endsWith method on this shortened string. Let’s check.
let bool = str.endsWith('my', 10); console.log(bool); //true
Yes, it works!
So, this way, you can shorten your search, and validate the ending of a substring instead of the entire string.
Make the search case insensitive
As we saw in with the startsWith method in the previous tutorial, these searches are not case sensitive.
let bool = str.endsWith('My', 10); console.log(bool); //false
We can make our search case insensitive by converting both the string, and the substring we’re searching for into lowercase by using the toLowerCase() method, thereby standardizing the string to avoid false negatives.
let bool = str.toLowerCase().endsWith('My'.toLowerCase(), 10); console.log(bool); //true
Unfortunately, regular expressions don’t work with endsWith, so we can’t use the case insensitive flag with regular expressions to make our life easier.