Open In App

What is endsWith() Method in JavaScript ?

In JavaScript, the endsWith() method is used to determine whether a string ends with a specified suffix (another string). It returns true if the string ends with the specified suffix, and false otherwise.

Syntax:

string.endsWith(suffix, length)

Example: Here, the endsWith() method is called on the str string to check if it ends with the suffix “World!”. Since “World!” is present at the end of the string, the method returns true, which is then assigned to the variable endsWithWorld.




const str = "Hello, World!";
const endsWithWorld = str.endsWith("World!");
 
console.log(endsWithWorld); // Output: true

Output
true

Note:

Article Tags :