JavaScript .endsWith()

joha_na's avatar
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.

  • 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

Syntax

string.endsWith(searchString, endPosition);

Parameters:

  • searchString: The characters to search for at the end of string.
  • length (optional): If provided, it considers only the first length characters 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:

true
false
true

Codebyte Example

This codebyte example is runnable and checks whether a string, up to the specified position, ends with the given search string:

Code
Output
Loading...

All contributors

Contribute to 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