Tutorials Tonight's Logo

JavaScript endsWith Method To Check String End

In this tutorial, you will learn about endsWith() method in JavaScipt and will know how to find a string with its ending.

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

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

JavaScipt endsWith method

JavaScipt endsWith Method

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

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

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

const str1 = "Learning javascript";
console.log(str1.endsWith("javascript")); // true
const str2 = "How fast is javascript?";
console.log(str2.endsWith("?")); // true
▶ Try It

Syntax of endsWith method

The syntax of javascript endsWith() string method is:

string.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 string end at index 10 then set the length argument to it as 10 + 1 (since the index is 10 so length become 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 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.


The endSwith() method is case sensitive, which means it treats string with the same characters but the different case 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'.");
▶ Try It

Points to remember:

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

Copyright © Tutorials Tonight. All Rights Are Reserved.