JavaScript Tutorials

JavaScript String endsWith()
Syntax & Examples

String.endsWith() method

The endsWith() method of the String class in JavaScript determines whether a string ends with the characters of the specified string, searchString. It can also check within a specific portion of the string by providing an endPosition parameter.


Syntax of String.endsWith()

There are 2 variations for the syntax of String.endsWith() method. They are:

1.
endsWith(searchString)

Parameters

ParameterOptional/RequiredDescription
searchStringrequiredThe characters to be searched for at the end of this string.

This method determines whether a string ends with the characters of the string searchString.

Returns value of type Boolean.

2.
endsWith(searchString, endPosition)

Parameters

ParameterOptional/RequiredDescription
searchStringrequiredThe characters to be searched for at the end of this string.
endPositionoptionalThe position within the string at which to end the search. Defaults to the string's length.

This method determines whether a string ends with the characters of the string searchString, considering only the portion of the string up to endPosition.

Returns value of type Boolean.



✐ Examples

1 Using endsWith() method to check the end of a string

In JavaScript, we can use the endsWith() method to check if a string ends with a specified substring.

For example,

  1. We define a string variable str with the value 'Hello, world!'.
  2. We use the endsWith() method with the argument 'world!' to check if the string ends with 'world!'.
  3. The result is stored in the variable result.
  4. We log result to the console using the console.log() method.

JavaScript Program

const str = 'Hello, world!';
const result = str.endsWith('world!');
console.log(result);

Output

true

2 Using endsWith() method with a specified end position

In JavaScript, we can use the endsWith() method to check if a string ends with a specified substring, up to a certain position.

For example,

  1. We define a string variable str with the value 'Hello, world!'.
  2. We use the endsWith() method with the arguments 'Hello' and 5 to check if the substring from the start to position 5 ends with 'Hello'.
  3. The result is stored in the variable result.
  4. We log result to the console using the console.log() method.

JavaScript Program

const str = 'Hello, world!';
const result = str.endsWith('Hello', 5);
console.log(result);

Output

true

3 Using endsWith() method to check for a false condition

In JavaScript, we can use the endsWith() method to demonstrate a case where the string does not end with the specified substring.

For example,

  1. We define a string variable str with the value 'Hello, world!'.
  2. We use the endsWith() method with the argument 'Hello' to check if the string ends with 'Hello'.
  3. The result is stored in the variable result.
  4. We log result to the console using the console.log() method.

JavaScript Program

const str = 'Hello, world!';
const result = str.endsWith('Hello');
console.log(result);

Output

false

Summary

In this JavaScript tutorial, we learned about endsWith() method of String: the syntax and few working examples with output and detailed explanation for each example.