String.prototype.lastIndexOf()

Returns the index of the last occurrence of a substring, or -1 if absent.

Since ES1 Spec ↗

Syntax

str.lastIndexOf(searchString, position)

Parameters

NameTypeRequiredDescription
searchString string Yes The substring to search for.
position number No The index at or before which to search, scanning backwards. Defaults to +Infinity.

Returns

number — The index of the last match, or -1 if not found.

Examples

console.log('abcabc'.lastIndexOf('b'));
Output
4
console.log('abc'.lastIndexOf('z'));
Output
-1

Notes

Case-sensitive. The match itself must start at or before `position`. Returns the absolute forward index of the match.

Browser & runtime support

EnvironmentSince version
chrome 1.0
firefox 1.0
safari 1.0
edge 12
node 0.10

See also