Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JavaScript · Function

indexOf

Array.prototype.indexOf() returns the first index where a value occurs in an array, or -1 when it is absent. It optionally accepts a starting index and uses strict equality for matching.

How it works

Call this method with a value to search an array from the beginning, or provide a starting index. It checks elements in ascending order with strict equality, returns the first matching position, and returns -1 when no match exists; sparse-array holes are skipped.

Syntax

indexOf(searchElement)
indexOf(searchElement, fromIndex)

Parameters

NameTypeDescription
searchElementanyThe value to locate in the array.
fromIndexintegerOptional zero-based starting index, converted to an integer. Negative values count from the end.

Returns

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

Key points

  • The result identifies the earliest matching position, or signals that the value is absent with -1.
  • The method accepts the value to locate and an optional starting position.
  • fromIndex is zero-based and converted to an integer before searching begins.
  • Negative starting positions are interpreted relative to the array's end.
  • Values are matched with strict equality rather than coercive equality.
  • The operation can be borrowed for suitable array-like objects.

Common mistakes

  • A search for NaN always fails because strict equality does not consider NaN equal to itself.
  • It cannot locate a sparse-array hole, because empty slots are not visited.
  • A starting index at or beyond the array length immediately produces -1.

Example: Find an array element's index

const fruits = ["Apple", "Banana", "Apple"];

const position = fruits.indexOf("Banana");
console.log(position); // 1
console.log(fruits.indexOf("Cherry")); // -1
console.log(fruits.indexOf("Apple", 1)); // 2

This finds the first matching value, reports absence with -1, and can begin searching at a specified index.

When to use it

Membership with a position

Check whether a value occurs while retaining its position when present.

Search from an offset

Find a later occurrence by beginning the search after an earlier match.

Handle array-like data

Apply the method to objects that expose length and integer-keyed properties.

Interview questions

QWhat does Array.prototype.indexOf return?

It returns the first index where the searched value occurs, or -1 when no element matches. Matching uses strict equality, equivalent to the === comparison algorithm.

Follow-up: How would you find NaN? Use Number.isNaN while scanning, or use includes when only a presence check is needed, because indexOf(NaN) always returns -1.

QHow does fromIndex affect the search?

The optional fromIndex is converted to an integer and determines where the search starts. A negative value is counted from the end, while a value beyond the array length prevents searching.

Follow-up: What happens with a negative fromIndex smaller than -array.length? It is treated as 0, so the complete array is searched.

QIs indexOf limited to Array instances?

The method is generic: it only requires a this value with a length property and integer-keyed properties. Therefore, it can operate on array-like objects as well as actual arrays.

Follow-up: Does generic mean it works on every object? No; the object still needs a usable length and indexed properties for meaningful results.

  • Array.prototype.includes()
  • Array.prototype.lastIndexOf()
  • Array.prototype.findIndex()
  • TypedArray.prototype.indexOf()

Source: Array.prototype.indexOf() - JavaScript - MDN Web Docs. Checked 2026-10-15.