# How to get the last index of an occurrence of the specified value in a string in JavaScript?

JavascriptWeb DevelopmentFront End Technology

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

The following is the syntax −

string.lastIndexOf(searchValue[, fromIndex])

Here are the 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.

## Example

You can try to run the following code to learn how to get an index of an occurrence of the specified value in a string −

Live Demo

<html>
<title>JavaScript String lastIndexOf() Method</title>

<body>
<script>
var str1 = new String( "Learn for free and enjoy! Learn and share!" );
var index = str1.lastIndexOf( "Learn" );
document.write("lastIndexOf found String :" + index );
document.write("<br />");
var index = str1.lastIndexOf( "and" );
document.write("lastIndexOf found String :" + index );
</script>
</body>
</html>
Published on 18-Jan-2018 12:09:17