Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Array.prototype.lastIndexOf: searching backward

lastIndexOf finds the last matching index by searching existing elements backward from a chosen position.

Version: ECMAScript 2026 draft; example target: Node.js 24.22.2

Syntax

array.lastIndexOf(searchElement[, fromIndex])

array.lastIndexOf(searchElement[, fromIndex]) returns the greatest matching index or -1. It uses Strict Equality (===), so NaN does not match even NaN, while +0 matches -0. Sparse holes are skipped rather than treated as undefined.

Without fromIndex, the search starts at length - 1. After integer conversion, a value at least equal to the length starts at the last index; a negative value is relative to the end, and a result still below zero returns -1. Length is captured before searching. The method does not mutate and is generic for array-like receivers; null or undefined throws TypeError, and getter failures propagate.

Examples

const sparse = [, "x", NaN, "x"];
console.log(sparse.lastIndexOf("x"), sparse.lastIndexOf("x", 2));
console.log(sparse.lastIndexOf(undefined), sparse.lastIndexOf(NaN));
console.log(["a", "b", "a"].lastIndexOf("a", -2));

Result

3 1
-1 -1
0

Sources