• JavaScript Video Tutorials

JavaScript String lastIndexOf() Method



The JavaScript String lastIndexOf() method is used to find the index of the last occurrence of the specified substring in the original string. It returns the position of the substring, or -1 if the substring is not present. For example, "hello".lastIndexOf("ll") returns 2, while "hello".lastIndexOf("hi") returns -1.

This method accepts an optional parameter named position, which specifies the position in the original string from where the method starts searching for the specified substring. The default value of this parameter is 0. For example, "hello".lastIndexOf("l", 2) returns 2, while "hello".lastIndexOf("l", 3) returns 3.

Note − This method is case sensitive, it treats "a" and "A" as different values.

Syntax

Following is the syntax of JavaScript String lastIndexOf() method −

lastIndexOf(searchString, position)

Parameters

  • searchString − The substring to be searched.
  • position (optional) − The position to start from searching.

Return value

This method returns an index of the last occurrence of substring found.

Example 1

If we omit the position parameter, this method considers the default value 0 for position and returns the index of the last occurrence of the specified substring "t" in the original string "TutorialsPoint".

<html>
<head>
<title>JavaScript String lastIndexOf() Method</title>
</head>
<body>
<script>
   const str = "TutorialsPoint";
   const sub_str = "t";
   document.write("Original string: ", str);
   document.write("<br>Sub-string to be searched: ", sub_str);
   document.write("<br>An index of search string '", sub_str,"' is: ", str.lastIndexOf(sub_str));
</script>
</body>
</html>

Output

The above program returns an index as 13 for substing "t".

Original string: TutorialsPoint
Sub-string to be searched: t
An index of search string 't' is: 13

Example 2

If we pass the position parameter value as 5, the method will start searching for the substring from the position 5 in the original string.

Following is another example of the JavaScript String lastIndexOf() method. Here, we are using this method to find the index of the substring "o" at the starting position 5 in the original string "HelloWorld".

<html>
<head>
<title>JavaScript String lastIndexOf() Method</title>
</head>
<body>
<script>
   const str = "HelloWorld";
   const sub_str = "o";
   const position = 5;
   document.write("Original string: ", str);
   document.write("<br>Sub-string to be searched: ", sub_str);
   document.write("<br>Position value is: ", position);
   document.write("<br>An index of search string '", sub_str,"' is: ", str.lastIndexOf(sub_str, position));
</script>
</body>
</html>

Output

After executing the above program, it returns an index value of '4' for the substring 'o' at the specified position 5.

Original string: HelloWorld
Sub-string to be searched: o
Position value is: 5
An index of search string 'o' is: 4

Example 3

If the specified substring is not found in the original string, this method returns -1.

In the Following example, we are trying to find an index of sub-string "Mom", in the original string "JavaScript".

<html>
<head>
<title>JavaScript String lastIndexOf() Method</title>
</head>
<body>
<script>
   const str = "JavaScript";
   const sub_str = "Mom";
   document.write("Original string: ", str);
   document.write("<br>Sub-string to be searched: ", sub_str);
   let result = str.lastIndexOf(sub_str);
   document.write("<br>Method str.lastIndexOf(sub_str) returns: ", result);
   if(result == -1){
      document.write("<br>Not found...!");
   } else{
      document.write("<br>Found...!");
   }
</script>
</body>
</html>

Output

The lastIndexOf() method in the above program returns -1, when the substring is not found.

Original string: JavaScript
Sub-string to be searched: Mom
Method str.lastIndexOf(sub_str) returns: -1
Not found...!

Example 4

Let's see the difference between the indexOf() and lastIndexOf() methods by executing the following program.

<html>
<head>
<title>JavaScript String lastIndexOf() Method</title>
</head>
<body>
<script>
   const str = "JavaScript";
   const sub_str = "a";
   const position = 20;
   document.write("Original string: ", str);
   document.write("<br>Sub-string to be searched: ", sub_str);
   document.write("<br>Position value: ", position);
   let result = str.lastIndexOf(sub_str, position);
   document.write("<br>Method str.lastIndexOf(sub_str) returns: ", result);
</script>
</body>
</html>

Output

The indexOf() and lastIndexOf() methods return different index values for the same sub-string as −

Original string: JavaScript
Sub-string to be searched: a
Position value: 20
Method str.lastIndexOf(sub_str) returns: 3
Advertisements