Skip to main content
Version: 3.27.2

Array.prototype.lastIndexOf()

The lastIndexOf() method 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.

Syntax

lastIndexOf(searchElement)
lastIndexOf(searchElement, fromIndex)

Parameters

  • searchElement
    • : Element to locate in the array.
  • fromIndex optional
    • : Zero-based index at which to start searching backwards, converted to an integer.
      • Negative index counts back from the end of the array — if fromIndex < 0, fromIndex + array.length is used.
      • If 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.
      • If fromIndex >= array.length or fromIndex is omitted, 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.

Return value

The last index of the element in the array; -1 if not found.

Description

The lastIndexOf() method compares searchElement to elements of the array using strict equality (the same algorithm used by the === operator).

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.