What is the endsWith() method in JavaScript?

The endsWith() method in JavaScript determines if a string ends with the specified string’s characters. It then returns true or false as appropriate.

Syntax

str.endsWith(searchString[, length])
  • searchString: The characters to be searched for at the end of str.
  • length: Optional. It is used as the length for str and defaults to str.length.

Returns true if the given characters are found at the end of the string; otherwise, it false.

This method is case-sensitive.

Code

let str = 'Hello to the world.'
console.log(str.endsWith('world.')) // true
console.log(str.endsWith('WORLD.')) // false
console.log(str.endsWith('to the')) // false
console.log(str.endsWith('to the', 12)) // true

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved