JavaScript String - lastIndexOf() Method

Description

This method returns the index within the calling String object of the last occurrence of the specified value, starting the search at fromIndex or -1 if the value is not found.

Syntax

Its syntax is as follows −

string.lastIndexOf(searchValue[, fromIndex])

Argument Details

Return Value

Returns the index of the last found occurrence, otherwise -1 if not found.

Example

Try the following example.

<html>
   <head>
      <title>JavaScript String lastIndexOf() Method</title>
   </head>
   
   <body>
   
      <script type="text/javascript">
         var str1 = new String( "This is string one and again string" );
         var index = str1.lastIndexOf( "string" );
         document.write("lastIndexOf found String :" + index ); 
         
         document.write("<br />");
         
         var index = str1.lastIndexOf( "one" );
         document.write("lastIndexOf found String :" + index ); 
      </script>
      
   </body>
</html>

Output

lastIndexOf found String :29
lastIndexOf found String :15