Skip to main content
Version: 3.27.2

Array.prototype.indexOf()

The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

Syntax

indexOf(searchElement)
indexOf(searchElement, fromIndex)

Parameters

  • searchElement
    • : Element to locate in the array.
  • fromIndex optional
    • : Zero-based index at which to start searching, converted to an integer.
      • Negative index counts back from the end of the array — if fromIndex < 0, fromIndex + array.length is used. Note, the array is still searched from front to back in this case.
      • If fromIndex < -array.length or fromIndex is omitted, 0 is used, causing the entire array to be searched.
      • If fromIndex >= array.length, the array is not searched and -1 is returned.

Return value

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

Description

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

The indexOf() method skips empty slots in sparse arrays.

The indexOf() method is generic. It only expects the this value to have a length property and integer-keyed properties.