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

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

Syntax

array.lastIndexOf(searchElement, fromIndex)

Parameters

ParameterDescription
searchElement The value to look for.
fromIndex optional Index to start searching backwards from.

Return value

An index, or -1.

Examples

Last occurrenceTry it yourself
const nums = [2, 5, 9, 2];
console.log(nums.lastIndexOf(2), nums.lastIndexOf(7));

Output

3 -1
Search before a positionTry it yourself
const path = ["home", "user", "docs", "user"];
console.log(path.lastIndexOf("user", 2));

Output

1

Version history

VersionChange
JavaScript ES5Added

Was this page helpful?