JavaScript String endsWith() Method



The JavaScript String endsWith() method is used to determine whether the string is ends with the specified characters (substring). It returns a boolean value 'true', if the specified substring characters found at the end of the string; otherwise, it returns 'false'.

This method throws a TypeError exception if the searchString parameter is a regular expression. It is case sensitive, so it treats "Hello" and "hello" as different strings.

Syntax

Following is the syntax of JavaScript String endsWith() method −

endsWith(searchString, endPosition)

Parameters

This method accepts two parameters: "searchString" and "endPosition". The endPosition parameter is optional and specifies the index (position) within the string where the search ends.

  • searchString − The characters to be searched for at the end of the string.
  • endPosition (optional) − The position within the string where the search ends.

Return value

This method returns true if the string ends with the specified substring, and false otherwise.

Example 1

If the string ends with the specified characters, it returns true.

In this example, we use the JavaScript String endsWith() method to check whether the string "TutorialsPoint" ends with the specified substring "Point".

<html>
<head>
<title>JavaScript String endsWith() Method</title>
</head>
<body>
<script>
   const str = "TutorialsPoint";
   const searchString = "Point";
   document.write("Original string: ", str);
   document.write("<br>Search string: ", searchString);
   document.write("<br>Is string '", str, "' ends with '", searchString, "'or not? ", str.endsWith(searchString));
</script>
</body>
</html>

Output

The above program returns 'true'.

Original string: TutorialsPoint
Search string: Point
Is string 'TutorialsPoint' ends with 'Point'or not? true

Example 2

If the string does not ends with the specified characters, it returns false.

Here is another example of the JavaScript String endsWith() method. In this example, we use this method to determine whether the string "Hello World" ends with the specified string "Word".

<html>
<head>
<title>JavaScript String endsWith() Method</title>
</head>
<body>
<script>
   const str = "Hello World";
   const searchString = "Word";
   document.write("Original string: ", str);
   document.write("<br>Search string: ", searchString);
   document.write("<br>Is string '", str, "' ends with '", searchString, "' or not? ", str.endsWith(searchString));
</script>
</body>
</html>

Output

After executing the above program, it returns 'false'.

Original string: Hello World
Search string: Word
Is string 'Hello World' ends with 'Word' or not? false

Example 3

If we pass the endPosition parameter value as 15, it checks if the string ending at index 15 ends with the specified substring, and rest of the string is ignored. If the substring matches the end of searchString (up to index 15), it returns true; otherwise, it returns false.

<html>
<head>
<title>JavaScript String endsWith() Method</title>
</head>
<body>
<script>
   const str = "Welcome to Tutorials Point";
   let endPosition = 15;
   const searchString = "Tuto";
   document.write("Original string: ", str);
   document.write("<br>End position: ", endPosition);
   document.write("<br>Search string: ", searchString);
   document.write("<br>String actual length: ", str.length);
   document.write("<br>Is string '", str, "'(upto length 15) ends with '",searchString,"' or not? ", str.endsWith(searchString, endPosition));
</script>
</body>
</html>

Output

Once the above program is executed, it will return 'true'.

Original string: Welcome to Tutorials Point
End position: 15
Search string: Tuto
String actual length: 26
Is string 'Welcome to Tutorials Point'(upto length 15) ends with 'Tuto' or not? true

Example 4

The JavaScript String endsWith() method expects the searchString parameter to be a string. If it is a regular expression, it throws a 'TypeError' exception.

<html>
<head>
<title>JavaScript String endsWith() Method</title>
</head>
<body>
<script>
   const str = "Tutorials Point";
   const searchString = /ab+c/;
   document.write("Original string: ", str);
   document.write("<br>Search string: ", searchString);
   try {
      document.write("Is string '",str,"' is ends with '",searchString,"' or not? ", str.endsWith(searchString));
   } catch (error) {
      document.write("<br>", error);
   }
</script>
</body>
</html>

Output

The above program thorws a 'TypeError' exception.

Original string: Tutorials Point
Search string: /ab+c/
TypeError: First argument to String.prototype.endsWith must not be a regular expression
Advertisements