JavaScript String endsWith() Method

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

Overview

While programming in JavaScript, there may be a situation where the programmer needs to check the ending characters of the given string. The javascript endsWith method of the String class checks whether a given string ends with a specified set of character(s). This method searches for single or multiple characters at the end of the given string and returns true if the characters are found

Syntax of JavaScript String endsWith() Method

The syntax of the javascript endswith() method is as follows:

Parameters of JavaScript String endsWith() Method

The parameters of the javascript endswith() method are as follows: There are two parameters that the javascript endswith() method accepts:

  • searchvalue: The searchvalue parameter is a required parameter and it represents the string that we need to search for.
  • length: The length parameter is optional, and it determines the length of the given string value that needs to be searched.

Return Value of JavaScript String endsWith() Method

Return Value: Boolean

The javascript endswith() method is a string method that returns the boolean value only. If the string ends with the given character or the string value, then the method returns true, otherwise, the method returns false.

Exceptions of JavaScript String endsWith() Method

The endswith() method throws a NullPointerException error when a null is passed to the string as a suffix. As this method is a string method in javascript, if the passed value is of the "integer" type or any other data type except the "string", then the method throws an error named incompatible types.

Example

Now let us see an example of the javascript endswith() method to understand the method's working in a program.

Output:

Explanation: In the above example, we have declared the variable named check_string and assigned the value of string TechOnTheNet. In the console.log, we used the javascript endswith() method with the variable named check_string by appending them using a dot (.) operator. In the first statement of console.log, the given string from where the searching of the string will take place is TechOnTheNet, and the search string is Net whose N is in the upper case that matches with the string parameter. So, the resultant output is true. But it is not the same in the second case, and hence the method returns the value false as the output.

What is JavaScript String endsWith() Method?

The javascript endswith() is used for finding a specified string that ends with a particular character or not. This method returns a boolean value as the return value. For checking the end character of the string, the ES6 developed a method known as the javascript endswith() method. This method searches for single or multiple characters at the end of the given string.

The javascript endswith() method accepts two parameters and one return type. The two parameters are searchvalue and length. The searchvalue represents the string we need to search for. This parameter is a required parameter. and the length represents the length of the given string value. This parameter is also optional.

Talking about the working of the javascript endswith() method. This method needs a required argument passed to the endswith() method. This argument helps in searching for whether the single character or multiple characters in the given string match or not. Then the method returns a boolean value that is True or False as the output according to the action performed. If the passed parameter passed matches with the given string then the method returns True as output, and if the passed parameter does not match with the given string, then the method returns False as the output.

Program to check if the email-id provided by the student is college-id or regular-id using the javascript endswith():

Explanation: Here, we are using the javascript endswith() method to check some conditions. We created two variables there, check_mail1 and check_mail2. Then we used console.log to print the output result after checking the string given in the string parameter from the reference parameters. In the first variable, the method checks for the string @harvard.edu in the reference string and then returns the boolean value according to the analogy. Since the conditions satisfy the first variable, the method will return true. And in the second variable, the condition is not satisfied, so the method returns false.

More Examples

In the above sections, we have discussed the various aspects related to the javascript endswith() method. Now let us see some examples of this method, where we will use the javascript endswith() method in different cases.

Example 1. Using endswith() to Check for Case-Sensitive Strings

Output:

Explanation: In the above example, we used the javascript endswith()method to check whether or not the given string ends with the specified string.

In check, the reference string is not matching with the arguments passed in the check. Hence, the method returns the boolean value true. And in the second case check1, the argument passed is not matched with the reference string in sentence. Hence the method returns the boolean value false.

Example 3. Using endswith on a Portion of the String

Output:

Explanation: In the above example, first, we created a reference string namely sentence and provided the value "JavaScript is fun" to it. Then we created a variable check and passed the JavaScript argument to it for checking. The second argument, 10 represents the portion of the string that needs to be checked. Since " JavaScript " is present in the first 10 characters of the (string) sentence, the condition becomes true and hence the method returns the boolean value true.

Supported Browsers

The javascript endswith() method is supported by almost all modern browsers. Given below are the name of some popular browsers that supports the javascript endswith() method.

Browser NameSupported on versions
Mozilla Firefox17.0 and above
Microsoft Edge12.0 and above
Google Chrome41 and above
Safari9.0 and above
Opera28.0 and above

Conclusion

  • The javascript endswith() is used for finding a specified string that ends with a particular character or string or not.
  • The syntax of the javascript endswith() method is : string.endsWith(searchvalue, length).
  • There are two parameters of the javascript endswith() method, that is searchvalue and the length.
  • The search value represents the string that we need to search for. This parameter is required, and the lengthrepresents the length of the given string value. This parameter is also optional.
  • The javascript endswith() method returns the boolean value only. If the string ends with the given character or the string value, then the method returns True. Otherwise, the method returns False.
  • Various modern browsers that support the javascript endswith() method are Mozilla Firefox, Microsoft Edge, Google Chrome, Safari, and Opera browser.