Check whether a string ends with another string in JavaScript
This post will discuss how to check whether a string ends with another string in JavaScript.
1. Using endsWith() method
The recommended solution is to use the JavaScript native method endsWith() to determine whether the string begins with a specified string. This is demonstrated below:
|
1 2 |
var str = 'Hello World'; console.log(endsWith(str, 'World')); // true |
This method was added to the ES6 specification and may not be available in all JavaScript implementations yet. However, you can use the following polyfill from MDN:
|
1 2 3 4 5 6 7 8 9 10 |
if (!endsWith) { function endsWith(search, this_len) { if (this_len === undefined || this_len > str.length) { this_len = str.length; } return str.substring(this_len - search.length, this_len) === search; }; } |
Alternatively, you can use the Lodash or Underscore.string library, that also offers the _.endsWith method. It checks whether the string ends with a given target string.
|
1 2 3 4 |
var _ = require('underscore.string'); // or, use lodash.js var str = 'Hello World'; console.log(_.endsWith(str, 'World')); // true |
2. Using lastIndexOf() method
Here, the idea is to find the index of the last occurrence of the given string. The following code example shows how to implement this using the lastIndexOf() method.
|
1 2 3 4 5 6 |
function endsWith(str, suffix) { return str.lastIndexOf(suffix) === str.length - suffix.length; }; var str = 'Hello World'; console.log(endsWith(str, 'World')); // true |
Since the lastIndexOf() method returns -1, the code will fail when the string’s length is one less than the length of the target substring. Here’s how to handle this:
|
1 2 3 4 5 6 7 |
function endsWith(str, suffix) { return str.length >= suffix.length && str.lastIndexOf(suffix) === str.length - suffix.length; }; var str = 'Hello World'; console.log(endsWith(str, 'World')); // true |
Alternatively, with the indexOf() method, you can do like:
|
1 2 3 4 5 6 |
function endsWith(str, suffix) { return str.indexOf(suffix, str.length - suffix.length) !== -1; }; var str = 'Hello World'; console.log(endsWith(str, 'World')); // true |
3. Using substring() or slice() method
The substring() method is used to get the string between the specified indexes. This can be used as follows:
|
1 2 3 4 5 6 |
function endsWith(str, suffix) { return str.substring(str.length - suffix.length) === suffix; }; var str = 'Hello World'; console.log(endsWith(str, 'World')); // true |
Alternatively, the slice() method can be called in place of the substring() method. The advantage of using the slice() method over the substring() method is that you can also use the negative indexes with it, as demonstrated below:
|
1 2 3 4 5 6 |
function endsWith(str, suffix) { return str.slice(-suffix.length) === suffix; }; var str = 'Hello World'; console.log(endsWith(str, 'World')); // true |
4. Using Regex
Another plausible way to check whether a string ends with another substring is by using regular expressions. Be careful with this approach as Regexes are costly, and you might run into performance problems.
|
1 2 3 4 5 6 |
function endsWith(str, suffix) { return new RegExp(suffix + '$').test(str); }; var str = 'Hello World'; console.log(endsWith(str, 'World')); // true |
Or use match() method, which match a string against a regular expression:
|
1 2 3 4 5 6 |
function endsWith(str, suffix) { return str.match(suffix + '$') !== null; }; var str = 'Hello World'; console.log(endsWith(str, 'World')); // true |
That’s all about determining whether a string ends with another string in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)