JavaScript String endsWith Method


In this tutorial, you will learn about endsWith() method in JavaScript and will know whether or not a string ends with a specified substring or not.

How will you identify whether a given sentence is a question or not?
Simple! By watching the question mark symbol (?).

In JavaScript there is a direct way to identify whether a string ends with a substring or not, it is by using the endsWith() method.

JavaScript endsWith method

JavaScript endsWith Method

The endsWith() string method is used to determine whether a string ends with some given string or not.

If the string ends with a specified string then it returns true if not then return false.

The method is applied on a string and the check string is passed as the first argument to the method.

Example

var str1 = "Learning javascript";
// check if the string ends with "javascript"
console.log(str1.endsWith("javascript"));

var str2 = "How fast is javascript?";
// check if the string ends with "?"
console.log(str2.endsWith("?"));
Try It

Syntax of endsWith method

The syntax of javascript endsWith() string method is:

str.endsWith(search_string, length);

The method accepts 2 arguments:

  1. search_string - This is the substring or character to be searched at the end of the string.
  2. length (optional) - It is an optional argument that is used to define the length of the string. The default length used is string.length.

Let's you want to check the string end at index 10 then set the length argument to it as 10 + 1 (since the index is 10 so length becomes 11).

Example: Using length

const str1 = "Learning to code is learning to create and innovate";
console.log(str1.endsWith("javascript", 10 + 1)); // false
console.log(str1.endsWith("to", 10 + 1)); // true

const str2 = "How fast is javascript?";
console.log(str2.endsWith("?", 8)); // false
console.log(str2.endsWith("?", str2.length)); // true
Try It

In the above example, since we want to check the string end at index 10 so we used the length as 11 in the first part of the code. In the second part when we set string length as 8 and checked if it ends with a question mark (?) then it returns false, but when we set its length as string.length it returns true.

Case-sensitivity

The endSwith() method is case sensitive, which means it treats strings with the same characters but the different cases as different.

Look out the following example:

const str = "Learn to code";
console.log(str.endsWith("Code")); // false
console.log(str.endsWith("CODE")); // false
console.log(str.endsWith("code")); // true
Try It

Using The endsWith Method In A Function

Check out some random examples:

// Using endsWith() method in a function
function checkEnd() {
  const str = 'To do or not to do';
  const end1 = "do to";
  const end2 = "to do";
  str.endsWith(end1) ? console.log("String ends with 'do to'.")
                     : console.log("String doesn't ends with 'do to'.");
  str.endsWith(end2) ? console.log("String ends with 'to do'.")
                     : console.log("String doesn't ends with 'to do'.");
}
checkEnd();
Try It

Conclusion

The endsWith() check whether or not a string ends with a specified substring. The endsWith() method performs a case-sensitive search on the string.