JavaScript

Check if String Ends With Substring in JavaScript

Sometimes, programmers need to identify whether the created strings contain the specified string or start or end with the specified substring. To do this, in JavaScript, different predefined methods exist. In JavaScript, the “endsWith()” method is the most utilized method for identifying whether the substring is present at the end of the string.

This blog post will help to learn the procedure to check if a substring is present at the end of the string in JavaScript.

How to Check if String Ends With Substring in JavaScript?

To determine if the string ends with a substring, use the following methods:

Let’s check out these methods!

Method 1: Check if Substring Present at the End of the String Using endsWith() Method

Use the “endsWith()” method to check whether the string ends with the substring or not. It takes a substring that will be checked in the string, whether the string ends with it or not, as an argument. Its outputs “true” or “false” if the substring is present or not at the end of the string respectively.

Syntax

Follow the below-given syntax for the “endsWith()” method:

string.endsWith(searchString, length)

In the above syntax, the specified method takes two parameters:

  • The “searchString” is the searched string that will be searched in the string. It is a mandatory parameter.
  • length” is an optional attribute of the string, which means the default value is the length of the string.

Return Value

The endsWith() method outputs “true” when the string ends with the substring and “false” when it does not exist in the string.

Example

Create a string stored in a variable “string”:

var string = 'Learn JavaScript from Linuxhint';

Create a variable “substring” that stores a part of the string as a substring:

var substring = 'Linuxhint';

Call the “endsWith()” method with string and pass a substring in it as an argument, that will check whether the string ends with the specific substring or not:

var result = string.endsWith(substring);

Print the resultant value using the “console.log()” method:

console.log(result);

Output

The above output displays “true”, which indicates that the string ends with the specified substring.

Method 2: Check if Substring Present at the End of the String Using substring() Method

To determine whether the string ends with the substring, utilize the “substring()” method. It is utilized to retrieve the string between the specified indexes, so, subtract the length of the substring from the string’s length. If the returned string is the same as the specified substring, it is true, indicating that it ends with a substring.

Syntax

Use the given syntax to check whether the string ends with a substring or not with the help of the “substring()” method:

string.substring(string.length - subString.length) === subString;

In the above syntax, subtract the length of the substring from the length of the string, if the resultant string is equivalent to the specified substring, it means the string ends with a substring.

Return value

If a substring is present at the end of the string, it outputs “true”, else, “false” is returned.

Example

After specifying the string and substring, define a function “stringEnds()” with two parameters, the string “str” and the substring “subStr”, then, invoke the “substring()” method and return the resultant value to the function:

function stringEnd(str, subStr) {

return str.substring(str.length - subStr.length) === subStr;

};

Call the defined function by passing a string as a first argument that will be checked and substring as a second argument that needs to be searched at the end of the given string:

console.log(stringEnd(string, substring));

Output

The above output displays “true” which means, the string ends with the specified substring.

Method 3: Check if Substring Present at the End of the String Using indexOf() Method

Another method for determining whether the string ends with the substring or not is the “indexOf()” method. It gives the position of the first instance of a value in a string. For checking if the substring is present at the end of the string, it takes a “substring” and the difference of the string’s length with the substring’s length as parameters. If the resultant value equals “-1”, it means the substring does not present at the end of the string.

Syntax

Follow the given syntax for the “indexOf()” method:

string.indexOf(searchValue, string.length - searchValue.length) !== -1;

Here, “searchValue” is the “substring” that will be looked up at the string’s end.

Return Value

If the substring cannot appear in the string, it returns “-1”, else, it returns “1”.

Example

Define a function “stringEnds()” with two parameters, the string “str” and the substring “subStr”, then invoke the “indexOf()” method and returns the resultant value to the function:

function stringEnd(str, subStr) {

return str.indexOf(subStr, str.length - subStr.length) !== -1;

};

Invoke the defined function “stringEnd()” by passing a string and substring as arguments:

console.log(stringEnd(string, substring))

Output

All the relevant information is gathered related to identifying whether the string ends with a substring or not.

Conclusion

To determine if the string ends with the substring, use JavaScript predefined methods, including the “endsWith()” method, “substring()” method, or “indexOf()” method. All of these methods give back the boolean value “true” as an output if the string ends with the specified substring, else, it outputs “false”. This tutorial helps to learn the procedure for checking whether the string ends with a substring or not using JavaScript.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.