JS: String.prototype.endsWith()

Advertisements
Advertisements

The JavaScript String.prototype.endsWith() function will receive a string as parameter and check if the given string ends with the parameter string. If it does end with parameter string, returns true otherwise, returns false.

The syntax to use endsWith() is given below.

String = "My Name is James Bond";
String.endsWith("ond");

The string “ond” is matched with “My name is James Bond” and if the string ends with “ond”, a boolean true is returned.

Usage

<!DOCTYPE html>
<html>
<head>
   <title>String.prototype.endsWith(endStr)</title>
   <meta charset="utf-8">
   <style>
   main{
   
   width:70%;
   margin: 10% auto;
   padding:4%;
   background:steelblue;
   text-align:center;
   }
   
   #in,#inTwo{
   
   padding:5px;
   margin:5px;
   
   }
   button{
     padding:10px;
	 
   }
   
   </style>
</head>
<body>
<main>
<p>Program to check a string using String.prototype.endsWith(endStr) javascript function.</p>
<h1 id="out">Output here</h1><br/>
<input type="text" id="in" value= "Enter a string"/><br/>
<input type="text" id="inTwo" value="Enter string ends with"><br/>
<button onclick="checkEnd()">Check String</button>

</main>
<script>
function checkEnd()
{
var str = document.getElementById("in").value;
var endStr = document.getElementById("inTwo").value;
document.getElementById("out").innerHTML = str.endsWith(endStr);
}

</script>

</body>
</html>

Output To Browser: endsWith() function

Output - JavaScript endsWith() function
Output – JavaScript endsWith() function
Advertisements
Advertisements
Advertisements