Javascript Reference - JavaScript String lastIndexOf() Method








lastIndexOf() searches a string and return the position. It returns -1 if the substring isn't found. lastIndexOf() method does the search from the end of the string.


var stringValue = "hello world"; 
console.log(stringValue.lastIndexOf("o")); //7 

The code above generates the following result.

Browser Support

lastIndexOf() Yes Yes Yes Yes Yes

Syntax

stringObject.lastIndexOf(searchvalue, start) ;




Parameter Values

Parameter Description
searchvalue Required. The string to search for
start Optional. Where to start searching backwards. The default value is the length of the string

Return Value

Return the last index of searchvalue in the string. Or -1 if not found.

Start from middle

An optional second argument tells where to start with.


var stringValue = "hello world"; 
console.log(stringValue.lastIndexOf("o", 6)); //4 

The code above generates the following result.