JavaScript .endsWith()
Published Jul 10, 2025
Contribute to Docs
The .endsWith() JavaScript string method checks whether a string ends with a given substring, returning true if it does and false otherwise. It returns true when the specified string is empty (""). The method is case-sensitive.
Syntax
string.endsWith(searchString, endPosition);
Parameters:
searchString: The characters to search for at the end ofstring.length(optional): If provided, it considers only the firstlengthcharacters of the string.
Example
In this example, the .endsWith() method checks if a string ends with a given substring, optionally considering only a portion of the string based on the provided length:
console.log('Hello, World! This is JavaScript.'.endsWith('JavaScript.'));console.log('Hello, World! This is JavaScript.'.endsWith('JavaScript'));console.log('Hello, World! This is JavaScript.'.endsWith('World', 12));
The output of this code is:
truefalsetrue
Codebyte Example
This codebyte example is runnable and checks whether a string, up to the specified position, ends with the given search string:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
- Includes 34 Courses
- With Professional Certification
- Beginner Friendly.115 hours
- Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
- Beginner Friendly.15 hours