JavaScript LastIndexOf

The JavaScript LastIndexOf method is used to return the index position of the last occurrence of a specified string. It will return -1 if the specified string not found. The syntax of the JavaScript LastIndexOf function is

String_Object.LastIndexOf(Substring, Starting_Position)
  • String_Object: Please select the valid String Object or literal.
  • Substring: Please specify the string you want to search for.
  • Starting_Position: This is an optional parameter. If you want to specify the starting point (starting index position) then, Please determine the index value here.

NOTE:

  • If we specify the Negative number as Starting_Position, JavaScript LastIndexOf() will start looking from Zero.
  • If we specify the Out of range Index as Starting_Position, LastIndexOf() will start looking from the Highest index number.

JavaScript LastIndexOf Example

The following set of examples will help you understand the LastIndexOf Function in JavaScript.

<!DOCTYPE html>
<html>
<head>
    <title> JavaScript LastIndexOf </title>
</head>
<body>
    <h1> JavaScript LastIndexOf </h1>
<script>
 var Str1 = "Learn JavaScript at Tutorial Gateway.org";
 var Str2 = "We are abc working at abc company";
 var Str3 = Str1.lastIndexOf("Script");
 var Str4 = Str1.lastIndexOf("abc"); // Non existing item
 var Str5 = Str2.lastIndexOf("abc");
 var Str6 = Str2.lastIndexOf("abc", 19);
 
 document.write("<b> Index position of Script is:</b> " + Str3);
 document.write("<br \> <b> Index position of abc is:</b> " + Str4);
 document.write("<br \> <b> Index position of abc is:</b> " + Str5);
 document.write("<br \> <b> Index position of abc is:</b> " + Str6);
</script>
</body>
</html>
JavaScript LastIndexOf

In this JavaScript LastIndexOf function example, Within the Script tag, First we declared two String variables Str1, Str2 and assigned corresponding values using following statement

 var Str1 = "Learn JavaScript at Tutorial Gateway.org";
 var Str2 = "We are abc working at abc company";

The following statement will find the Last index position of a substring “Script” and store the index value in Str3. You should count the space as One Character.

var Str3 = Str1.lastIndexOf("Script");

In the next line, we are looking for a non-existing item “abc” inside the Str1. Since JavaScript LastIndexof() function doesn’t find the substring, it is returning -1 as output

 var Str4 = Str1.lastIndexOf("abc"); // Non existing item

In the next line, we are looking for “abc” inside the Str2.

 var Str5 = Str2.lastIndexOf("abc");

From the above statement, you can observe that although term abc repeated multiple times, JS LastIndexof function is writing the index position of the Last occurrence. Now, let us change the starting position from default to 19

 var Str6 = Str2.lastIndexOf("abc", 19);

The above statement will return the last occurrence of string abc starting at index position 19. Placing the output content to the respective browser

 document.write("<b> Index position of Script is:</b> " + Str3);
 document.write("<br \> <b> Index position of abc is:</b> " + Str4);
 document.write("<br \> <b> Index position of abc is:</b> " + Str5);
 document.write("<br \> <b> Index position of abc is:</b> " + Str6);