Users Online

· Guests Online: 95

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

Functions - endsWith() in JavaScript

endsWith() in JavaScript

endsWith() in JavaScript

Introduction to endsWith() in JavaScript

In JavaScript, endsWith() is a String method, which is used to 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 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, then it searches the string up to given length value, otherwise, the method considers as default string length value. For example, if the length attribute value set to 6. This means that only the first six characters of the string will be searched or tested.
Note: In the above Syntax, the substringvalue attribute is mandatory, while length Attribute will be Optional. Like endsWith() method, if we verify a given string begins with the characters of a specified string, then we use the startsWith() method. This method is case sensitive. The endsWith() method is not supported in IE Browser (IE 11 & earlier versions).

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:

endsWith() in JavaScript eg1

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:

endsWith() in JavaScript eg1

In the above example the value of length parameter set to 11 i.e. 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:

endsWith() in JavaScript eg3

In the above example, it provides False output, Because the endsWith() method perform a Case-sensitive searching, 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:

endsWith() in JavaScript eg4

In the above example, we use the console class with the log method. By using this class we executed the given method and show 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.

The value of length attribute set to 11 i.e. it checks first 11characters 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. As the endsWith() method performs Case-sensitive testing, 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 #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:

eg4

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 first & second case 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:

eg6

In the above example, we have used the endsWith( ) method to check if the given string ends with the substringvalue of multiple words or not. Therefore the method returns true in the first case and false in the second case.

Conclusion

In this article we especially discussed on JavaScript string ends with the () method, which tests the substringvalue with the end of the given string & returns Boolean value i.e. true or false. Similarly, you can verify 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 which are related to String like, startsWith( ), replace( ), replace( ),substr( ),etc.

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.69 seconds
10,825,672 unique visits