javascript
  1. javascript-endswith

JavaScript endsWith()

The endsWith() method in JavaScript is a built-in string method that determines whether a string ends with a specified substring or character. This method returns a boolean value indicating whether or not the string ends with the substring specified.

Syntax

The basic syntax for using the endsWith() method is as follows:

string.endsWith(searchString, position)

Parameters:

  • searchString (required) - Specifies the substring to search for. This parameter is case-sensitive.
  • position (optional) - Specifies the position in the string at which to end the search. The endsWith() method searches the part of the string ending at the position specified. If this parameter is omitted, endsWith() searches the whole string.

Example

Consider the following example:

const text = 'Hello, World!';

console.log(text.endsWith('!'));       // true
console.log(text.endsWith('o'));       // false
console.log(text.endsWith('Hello'));   // false
console.log(text.endsWith('World', 7));   // true

Output

The output of the above example would be:

true
false
false
true


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>JavaScript Output</title>
</head>
<body>

<!-- Your JavaScript code goes here -->
<script>
  const text = 'Hello, World!';

  document.write('<p>' + text.endsWith('!') + '</p>');       // true
  document.write('<p>' + text.endsWith('o') + '</p>');       // false
  document.write('<p>' + text.endsWith('Hello') + '</p>');   // false
  document.write('<p>' + text.endsWith('World', 7) + '</p>'); // true
</script>

</body>
</html>
Try Playground

Explanation

In the above example, we use the endsWith() method to check whether a given string ends with a specified substring or character.

The first console.log() statement checks whether the text string ends with the '!' character. Since the text string ends with '!', the output would be true.

The second console.log() statement checks whether the text string ends with the 'o' character. Since the text string does not end with 'o', the output would be false.

The third console.log() statement checks whether the text string ends with the 'Hello' substring. Since the text string does not end with 'Hello', the output would be false.

The fourth console.log() statement checks whether the text string ends with the 'World' substring starting from the seventh character. Since the text string ends with 'World' in this substring, the output would be true.

Use

The endsWith() method is useful for tasks such as checking the file extension of a given file name or verifying the correctness of the user's input.

Important Points

  • The endsWith() method is case-sensitive.
  • If the position parameter is negative, endsWith() counts positions from the end of the string. For example, a position of -1 means the last character of the string, -2 means the second-last character of the string, and so on.
  • The endsWith() method returns true if the substring being searched is an empty string.

Summary

The endsWith() method in JavaScript is a useful method for verifying whether a given string ends with a specified character or substring. It takes in a required searchString argument, and an optional position argument to specify the end position of the search. It returns a boolean value indicating whether the string ends with the specified substring.

Published on: