| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/lastIndexOf | [Back] [Original] |
Get to know MDN better
This feature is well established and works across many devices and browser versions. Its been available across browsers since July 2015.
The lastIndexOf() method of Array instances returns the last index at which
a given element can be found in the array, or -1 if it is not present. The array is
searched backwards, starting at fromIndex.
const animals = ["Dodo", "Tiger", "Penguin", "Dodo"];
console.log(animals.lastIndexOf("Dodo"));
// Expected output: 3
console.log(animals.lastIndexOf("Tiger"));
// Expected output: 1
lastIndexOf(searchElement)
lastIndexOf(searchElement, fromIndex)
searchElementElement to locate in the array.
fromIndex OptionalZero-based index at which to start searching backwards, converted to an integer.
-array.length <= fromIndex < 0, fromIndex + array.length is used.fromIndex < -array.length, the array is not searched and -1 is returned. You can think of it conceptually as starting at a nonexistent position before the beginning of the array and going backwards from there. There are no array elements on the way, so searchElement is never found.fromIndex >= array.length or fromIndex is omitted or undefined, array.length - 1 is used, causing the entire array to be searched. You can think of it conceptually as starting at a nonexistent position beyond the end of the array and going backwards from there. It eventually reaches the real end position of the array, at which point it starts searching backwards through the actual array elements.The last index of searchElement in the array; -1 if not found.
The lastIndexOf() method compares searchElement to elements of the array using strict equality (the same algorithm used by the === operator). NaN values are never compared as equal, so lastIndexOf() always returns -1 when searchElement is NaN.
The lastIndexOf() method skips empty slots in sparse arrays.
The lastIndexOf() method is generic. It only expects the this value to have a length property and integer-keyed properties.
The following example uses lastIndexOf() to locate values in an array.
const numbers = [2, 5, 9, 2];
numbers.lastIndexOf(2); // 3
numbers.lastIndexOf(7); // -1
numbers.lastIndexOf(2, 3); // 3
numbers.lastIndexOf(2, 2); // 0
numbers.lastIndexOf(2, -2); // 0
numbers.lastIndexOf(2, -1); // 3
You cannot use lastIndexOf() to search for NaN.
const array = [NaN];
array.lastIndexOf(NaN); // -1
The following example uses lastIndexOf to find all the indices of an
element in a given array, using push() to add them
to another array as they are found.
const indices = [];
const array = ["a", "b", "a", "c", "a", "d"];
const element = "a";
let idx = array.lastIndexOf(element);
while (idx !== -1) {
indices.push(idx);
idx = idx > 0 ? array.lastIndexOf(element, idx - 1) : -1;
}
console.log(indices);
// [4, 2, 0]
Note that we have to handle the case idx === 0 separately here because the
element will always be found regardless of the fromIndex parameter if it is
the first element of the array. This is different from the
indexOf() method.
You cannot use lastIndexOf() to search for empty slots in sparse arrays.
console.log([1, , 3].lastIndexOf(undefined)); // -1
The lastIndexOf() method reads the length property of this and then accesses each property whose key is a nonnegative integer less than length.
const arrayLike = {
length: 3,
0: 2,
1: 3,
2: 2,
3: 5, // ignored by lastIndexOf() since length is 3
};
console.log(Array.prototype.lastIndexOf.call(arrayLike, 2));
// 2
console.log(Array.prototype.lastIndexOf.call(arrayLike, 5));
// -1
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-array.prototype.lastindexof |
Array.prototype.lastIndexOf in core-jsArray.prototype.lastIndexOfArrayArray.prototype.findIndex()Array.prototype.findLastIndex()Array.prototype.indexOf()TypedArray.prototype.lastIndexOf()String.prototype.lastIndexOf()This page was last modified on Jul 10, 2025 by MDN contributors.
Arrayat()concat()copyWithin()entries()every()fill()filter()find()findIndex()findLast()findLastIndex()flat()flatMap()forEach()includes()indexOf()join()keys()lastIndexOf()map()pop()push()reduce()reduceRight()reverse()shift()slice()some()sort()splice()toLocaleString()toReversed()toSorted()toSpliced()toString()unshift()values()with()[Symbol.iterator]()Object/FunctionYour blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |