JavaScript String endsWith() Method with Example

JavaScript String endsWith() Method: Here, we are going to learn about the endsWith() method in JavaScript with Example.
Submitted by IncludeHelp, on February 06, 2019

String endsWith() Method

endsWith() method is a string method in JavaScript, it is used to check whether a string ends with a specified substring or not.

It returns true – if string ends with a specified substring, and it returns false – if string does not end with the specified substring.

Syntax:

    String.endsWith(substring);

Examples:

    Input:
    str = "IncludeHelp is made for students.";
    substring = "students."
    //function call
    str.endsWith(substring);
    Output:
    True

    Input:
    str = "IncludeHelp is made for students.";
    substring = "Bye"
    //function call
    str.endsWith(substring);
    Output:
    False

Code:

<html>
<head>
<title>JavaScipt Example</title>
</head>

<body>
<script>
	var str = "IncludeHelp is made for students.";
	
	var substring = "students.";	
	if(str.endsWith(substring)){
		document.write(str + " ends with " + substring + "<br>");
	}
	else{
		document.write(str + " does not end with " + substring + "<br>");
	}

	substring = "Bye";
	if(str.endsWith(substring)){
		document.write(str + " ends with " + substring + "<br>");
	}
	else{
		document.write(str + " does not end with " + substring + "<br>");
	}	
</script>
</body>
</html>

Output

IncludeHelp is made for students. ends with students.
IncludeHelp is made for students. does not end with Bye

JavaScript String Object Methods »





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.