What is the endsWith() method in JavaScript?

The JavaScript endsWith() method is a string method that tests if a string ends with a specific substring. The endsWith() method was introduced in ECMAScript 5.1 and is defined in the String.prototype object.

The endsWith() method accepts two arguments: the string to test and the substring to search for. The endsWith() method returns true if the string ends with the substring, and false if the string does not end with the substring.

The code below demonstrates how to use the endsWith() method.

var str = "JavaScript";
var result = str.endsWith("script");
console.log(result); //true

The endsWith() method can be used to determine if a string ends with a specific character. The following code demonstrates how to use the endsWith() method to determine if a string ends with a specific character.

var str = "JavaScript";
var result = str.endsWith('t');
console.log(result); //true

The following code demonstrates how to use the endsWith() method to determine if a string ends with a specific substring.

var str = "JavaScript";
var result = str.endsWith("C");
console.log(result); //true

The following code demonstrates how to use the endsWith() method to determine if a string ends with a specific substring.

var str = "JavaScript";
var result = str.endsWith("lang");
console.log(result); //false
💡
Need a writer? Want to work with me? I have a subscription copywriting service available at Zipply.io. Subscribe to a set number of words and split it into as many copy assets as you need. Scale your startup, small business, or MVP. Upgrade, downgrade and pause at any time. Start your subscription here.