Tutorials Tonight
Follow Us

JavaScript lastIndexOf Method To Check Last Occurrence Of A Substring

In this tutorial, you will learn about the lastIndexOf() method in JavaScipt and will know how to get that last occurrence of a substring within a string.

JavaScript lastIndexOf String

The lastIndexOf() is used to find the last occurrence of a substring within another calling string.

It returns the last index value of the substring within the string. If no such substring is present in the string then it returns -1.

For efficiency of the method, it starts checking from the end of the string and return the first occurrence index from backward.

const string = "learning to code is learning to create and innovate";
const search_str = "learning";
console.log(string.lastIndexOf(search_str)); // 20
console.log(string.lastIndexOf("king")); // -1
▶ Try It

You can see in the above example the method returns the index of the last occurrence of the search string ("learning") which is 20 although it first occurs at index 0. Also when searching for the word "king" it returns -1.

Note: Even though the lastIndexOf() method searches the substring from backward, it still returns the location value of the substring relative to the front.

Syntax of lastIndexOf method

The syntax of javascript lastIndexOf() string method is:

string.lastIndexOf(search_value [ ,search_from])
  1. search_value - This is the substring or character to be searched within the given string.
  2. search_from (optional) - It is an optional argument that is used to specify the index from where the method starts searching the value (from backward). The default value is +Infinity.
javascript lastIndexOf Method

Since the method starts from backwards of the string so if you define the start_index to be 10, then it will start checking for the value from 10 to 0.

If search_from >= string.length then the whole string is searched. If search_from > 0 it would search for nothing.


const string = "to do or not to do";
console.log(string.lastIndexOf("do", 10)); // 3
console.log(string.lastIndexOf("do")); // 16
console.log(string.lastIndexOf("or", 8)); // 6
console.log(string.lastIndexOf("or")); // 6
▶ Try It

Note: 'tobetobe'.lastIndexOf('to', 4) will return 4 and not 0, as search_from limits only the beginning of the match.


The lastIndexOf() method is case sensitive.

See the following example:

const str = "To do or not to do";
console.log(str.lastIndexOf("To")); // 0
console.log(str.lastIndexOf("to")); // 13
▶ Try It

Using indexOf() and lastIndexOf() Method

The indexOf() method is similar to the lastIndexOf method but it returns the first matching string rather than the last.

Here is an example that search for the same word using 2 different methods.

const str = "To do or not to do";
console.log(str.indexOf("do")); // 3
console.log(str.lastIndexOf("do")); // 16
▶ Try It

Points to remember:

  1. The lastIndexOf() method find and return the last index of the specified substring from the given string.
  2. It is case sensitive.
  3. To get the first index of a specified string use the indexOf() method.