UNB/ CS/ David Bremner/ teaching/ cs2613/ books/ mdn/ Reference/ Global Objects/ Array/ Array.prototype.indexOf()

The indexOf() method of Array instances 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

Return value

The first index of searchElement 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). NaN values are never compared as equal, so indexOf() always returns -1 when searchElement is NaN.

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.

Examples

Using indexOf()

The following example uses indexOf() to locate values in an array.

const array = [2, 9, 9];
array.indexOf(2); // 0
array.indexOf(7); // -1
array.indexOf(9, 2); // 2
array.indexOf(2, -1); // -1
array.indexOf(2, -3); // 0

You cannot use indexOf() to search for NaN.

const array = [NaN];
array.indexOf(NaN); // -1

Finding all the occurrences of an element

const indices = [];
const array = ["a", "b", "a", "c", "a", "d"];
const element = "a";
let idx = array.indexOf(element);
while (idx !== -1) {
  indices.push(idx);
  idx = array.indexOf(element, idx + 1);
}
console.log(indices);
// [0, 2, 4]

Finding if an element exists in the array or not and updating the array

function updateVegetablesCollection(veggies, veggie) {
  if (veggies.indexOf(veggie) === -1) {
    veggies.push(veggie);
    console.log(`New veggies collection is: ${veggies}`);
  } else {
    console.log(`${veggie} already exists in the veggies collection.`);
  }
}

const veggies = ["potato", "tomato", "chillies", "green-pepper"];

updateVegetablesCollection(veggies, "spinach");
// New veggies collection is: potato,tomato,chillies,green-pepper,spinach
updateVegetablesCollection(veggies, "spinach");
// spinach already exists in the veggies collection.

Using indexOf() on sparse arrays

You cannot use indexOf() to search for empty slots in sparse arrays.

console.log([1, , 3].indexOf(undefined)); // -1

Calling indexOf() on non-array objects

The indexOf() 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: 4,
  3: 5, // ignored by indexOf() since length is 3
};
console.log(Array.prototype.indexOf.call(arrayLike, 2));
// 0
console.log(Array.prototype.indexOf.call(arrayLike, 5));
// -1

Specifications

Browser compatibility

See also