Array.prototype.indexOf()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
TheindexOf() method ofArray instances returns the first index at which agiven element can be found in the array, or -1 if it is not present.
In this article
Try it
const beasts = ["ant", "bison", "camel", "duck", "bison"];console.log(beasts.indexOf("bison"));// Expected output: 1// Start from index 2console.log(beasts.indexOf("bison", 2));// Expected output: 4console.log(beasts.indexOf("giraffe"));// Expected output: -1Syntax
indexOf(searchElement)indexOf(searchElement, fromIndex)Parameters
searchElementElement to locate in the array.
fromIndexOptionalZero-based index at which to start searching,converted to an integer.
- Negative index counts back from the end of the array — if
-array.length <= fromIndex < 0,fromIndex + array.lengthis used. Note, the array is still searched from front to back in this case. - If
fromIndex < -array.lengthorfromIndexis omitted,0is used, causing the entire array to be searched. - If
fromIndex >= array.length, the array is not searched and-1is returned.
- Negative index counts back from the end of the array — if
Return value
The first index ofsearchElement in the array;-1 if not found.
Description
TheindexOf() method comparessearchElement to elements of the array usingstrict equality (the same algorithm used by the=== operator).NaN values are never compared as equal, soindexOf() always returns-1 whensearchElement isNaN.
TheindexOf() method skips empty slots insparse arrays.
TheindexOf() method isgeneric. It only expects thethis value to have alength property and integer-keyed properties.
Examples
>Using indexOf()
The following example usesindexOf() to locate values in an array.
const array = [2, 9, 9];array.indexOf(2); // 0array.indexOf(7); // -1array.indexOf(9, 2); // 2array.indexOf(2, -1); // -1array.indexOf(2, -3); // 0You cannot useindexOf() to search forNaN.
const array = [NaN];array.indexOf(NaN); // -1Finding 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,spinachupdateVegetablesCollection(veggies, "spinach");// spinach already exists in the veggies collection.Using indexOf() on sparse arrays
You cannot useindexOf() to search for empty slots in sparse arrays.
console.log([1, , 3].indexOf(undefined)); // -1Calling indexOf() on non-array objects
TheindexOf() method reads thelength property ofthis and then accesses each property whose key is a nonnegative integer less thanlength.
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));// 0console.log(Array.prototype.indexOf.call(arrayLike, 5));// -1Specifications
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-array.prototype.indexof> |