Tutorials Point


  JavaScript Basics
  JavaScript Objects
  JavaScript Advanced
  JS Useful References
  JS Useful Resources
  Selected Reading

Copyright © 2014 by tutorialspoint



  Home     References     Discussion Forums     About TP  

Javascript String - lastIndexOf() Method


previous next AddThis Social Bookmark Button


Advertisements

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:

string.lastIndexOf(searchValue[, fromIndex])

Here is the detail of parameters:

  • searchValue : A string representing the value to search for.

  • fromIndex : The location within the calling string to start the search from. It can be any integer between 0 and the length of the string. The default value is 0.

Return Value:

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

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>

This will produce following result:

lastIndexOf found String :29
lastIndexOf found String :15  

To understand it in better way you can Try it yourself.



previous next Printer Friendly

Advertisements


  

Advertisements