Updated June 26, 2023
Introduction to endsWith() in JavaScript
In JavaScript, endsWith() is a String method that determines whether a string ends with the characters of a specified string or not. This method returns Boolean Value, i.e., true or false. It returns true if the string ends with the given characters or substring; otherwise false. This method is case-sensitive.
In JavaScript, the syntax for the endsWith() method is:
string.endsWith(substringvalue, length);
OR
string.endsWith(substringvalue);
Here, the endsWith() method accepts two Attributes or Parameters.
Parameters:
- string: This is a string Object.
- substringvalue: It is the set of characters to search for at the end of the given string. This is a mandatory attribute. It can be a single character or multiple characters, or multiple words.
- length: You can specify the length of the string you want it to search. This is an optional attribute. If the length attribute value is set, it searches the string up to the given length value; otherwise, the method considers it the default string length value. For example, if the length attribute value is set to 6. This means that only the first six characters of the string will be searched or tested.
Examples of endsWith() in JavaScript
Below are the examples of endsWith() in JavaScript:
Example #1 – Without Length Attribute
Code:
<!DOCTYPE html>
<html>
<body>
<script>
var str = "EDUCBA JavaScript Article";
var n = str. endsWith("Article");
document. write(n);
</script>
</body>
</html>
Output:
In the above example, we have used the endsWith() method to check if the given string ends with the substringvalue “Article” or not. Therefore the method returns true.
Example #2 – With Length Attribute
Code:
<!DOCTYPE html>
<html>
<body>
<script>
var str = "EDUCBA JavaScript Article";
var n = str. endsWith("Java",11);
document. write(n);
</script>
</body>
</html>
Output:
In the above example, the code sets the value of the length parameter to 11, which means it checks the first 11 characters in the given string. So if the substringvalue “Java” is found at the end of a given length of the string or not. Therefore the method returns true.
Example #3 – Case-Sensitive
Code:
<!DOCTYPE html>
<html>
<body>
<script>
var str = "EDUCBA JavaScript Article";
var n = str. endsWith("article");
document. write(n);
</script>
</body>
</html>
Output:
In the above example, it provides False output, Because the endsWith() method performs a Case-sensitive search, and the substringvalue “Article” is found at the end of the given string” EDUCBA JavaScript Article, “but “article” is not found. Therefore the method returns false.
Example #4 – Result on Browser Console
Code:
var str = "EDUCBA JavaScript Article";
console.log(str.endsWith("Article"));
console.log(str.endsWith("Java",11));
console.log(str.endsWith("article"));
Output:
We use the console class with the log method in the above example. Using this class, we executed the given method and showed all the results on Browser Console. The endsWith() method to test if the given string ends with the substringvalue “Article” or not. Therefore the method returns True.
It sets the value of the length attribute to 11 by checking the first 11 characters in the given string. Then, it determines whether the substring value “Java” is found at the end of the given length of the string. Therefore the method returns True. Since the endsWith() method performs case-sensitive testing, it finds the substring value “Article” at the end of the given string “EDUCBA JavaScript Article,” but it does not find “article.” Therefore the method returns False.
Example #5 – Single Character
Code:
var str = "EDUCBA JavaScript Article";
console.log(str.endsWith("e"));
console.log(str.endsWith("e"));
console.log(str.endsWith("f"));
Output:
In the above example, we have used the endsWith() method to check if the given string ends with the substringvalue of a single character or not. Therefore the method returns true in the first & second cases and false in the third case.
Example #6 – Multiple Characters
Code:
var str = "EDUCBA JavaScript Article";
console.log(str.endsWith("cle"));
console.log(str.endsWith("cee"));
Output:
In the above example, we have used the endsWith( ) method to check whether the given string ends with the substringvalue of multiple words. Therefore the method returns true in the first case and false in the second case.
Conclusion
In this article, we mainly discussed JavaScript string ends with the () method, which tests the substringvalue with the end of the given string & returns a Boolean value, i.e., true or false. Similarly, you can verify the JavaScript string startsWith( ) method, which tests or verify the substringvalue with the start of the given string and returns a Boolean value. So, there are so many methods that are related to string, like, startsWith( ), replace( ), replace( ),substr( ), etc.
Recommended Articles
This is a guide to the endsWith() in JavaScript. Here we discuss the syntax and parameters of endsWith() in JavaScript, along with examples and code implementation. You may also look at the following articles to learn more-