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.
lastIndexOf() |
Yes | Yes | Yes | Yes | Yes |
stringObject.lastIndexOf(searchvalue, start) ;
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 the last index of searchvalue
in the string.
Or -1 if not found.
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.