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
Syntax
string.lastIndexOf(searchString, position)
Parameters
| Parameter | Description |
|---|---|
searchString |
Text to look for. |
position optional |
Search backwards from this index. |
Return value
An index, or -1.
Examples
const path = "/home/ada/notes.txt";
console.log(path.slice(path.lastIndexOf("/") + 1));
Output
notes.txt
console.log("a.b.c".lastIndexOf("."), "abc".lastIndexOf("z"));
Output
3 -1
Version history
| Version | Change |
|---|---|
| JavaScript ES1 | Added |
In other languages
str.rfind()· Pythonstrrpos()· PHP
Related
string.indexOf()· Return the index of the first occurrence of some text, or -1 if it isn't found.string.slice()· Extract part of a string between two indexes; negative indexes count from the end.- JavaScript Strings · tutorial
- JavaScript String Methods · tutorial