JavaScript (ES2026) reference · Array Methods
JavaScript Array lastIndexOf() Method
Return the last index at which a value is found (using ===), searching backwards, or -1 if it isn't there.
JavaScript (ES2026) Since ES5
Syntax
array.lastIndexOf(searchElement, fromIndex)
Parameters
| Parameter | Description |
|---|---|
searchElement |
The value to look for. |
fromIndex optional |
Index to start searching backwards from. |
Return value
An index, or -1.
Examples
const nums = [2, 5, 9, 2];
console.log(nums.lastIndexOf(2), nums.lastIndexOf(7));
Output
3 -1
const path = ["home", "user", "docs", "user"];
console.log(path.lastIndexOf("user", 2));
Output
1
Version history
| Version | Change |
|---|---|
| JavaScript ES5 | Added |
Related
array.indexOf()· Return the first index at which a value is found (using ===), or -1 if it isn't in the array.array.findLastIndex()· Return the index of the last element that passes a test function, or -1 if none does.- JavaScript Arrays · tutorial
- JavaScript Array Methods · tutorial