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 ofstr.length: Optional. It is used as the length forstrand defaults tostr.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.')) // trueconsole.log(str.endsWith('WORLD.')) // falseconsole.log(str.endsWith('to the')) // falseconsole.log(str.endsWith('to the', 12)) // true
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved