Home » JavaScript

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




Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.