JavaScript String method : lastIndexOf(str)

The lastIndexOf() method returns the index position of the last occurrence of given value or word in a string. It is similar to the indexOf() method, but it returns the last occurrence instead of first occurrence. The syntax would be :
  str.lastIndexOf("value");
Example :
<html>
<body>
  <script>
    var mystr = "This is the Test String for the lastIndexOf() method.";
    var pos = mystr.lastIndexOf("the"); 
    document.write("Position of 'The' is : ", pos);
  </script> 
</body>
</html>
Output :


If the provided value is not found then it will return -1.

Next Topic :