JavaScript (ES2026) reference · String Methods

JavaScript String lastIndexOf() Method

Return the index of the last occurrence of some text, searching backwards, or -1 if it isn't found.

JavaScript (ES2026) Since ES1

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

string.lastIndexOf(searchString, position)

Parameters

ParameterDescription
searchString Text to look for.
position optional Search backwards from this index.

Return value

An index, or -1.

Examples

File name from a pathTry it yourself
const path = "/home/ada/notes.txt";
console.log(path.slice(path.lastIndexOf("/") + 1));

Output

notes.txt
Last match or -1Try it yourself
console.log("a.b.c".lastIndexOf("."), "abc".lastIndexOf("z"));

Output

3 -1

Version history

VersionChange
JavaScript ES1Added

In other languages

Was this page helpful?