Favorites

JavaScript Array.prototype.lastIndexOf() Method

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
Array.prototype.lastIndexOf()
JavaScript Array Instance methodThis method is supported by all modern browsers.
Updated 28th November 2026

The lastIndexOf() method returns the last index at which a value is found in an array, or -1.

Description

The lastIndexOf() method searches the array backwards and returns the index of the last occurrence of a value, using strict equality, or -1 if it isn't found.

Like indexOf(), it compares values with ===, so it can't find NaN and only finds objects that are the same reference. Empty slots in sparse arrays are skipped. The optional second argument sets where to start searching backwards from, and negative values count from the end.

A common use is checking whether a value appears more than once: if indexOf(x) and lastIndexOf(x) give different positions, it's a duplicate. To search backwards with a condition instead of an exact value, use findLastIndex().

Syntax

array.lastIndexOf(searchElement)
array.lastIndexOf(searchElement, fromIndex)

Parameters

  • searchElementThe value to look for.
  • fromIndexOptional. The index to start searching backwards from.

Return Value

The last index of the value, or -1 if it isn't found.

Example

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

Browser Support

The following information will show you the current browser support for the Array.prototype.lastIndexOf() method. Hover over a browser icon to see the version that first introduced support for this method.

This method is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee