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

JavaScript · Function

findIndex

Array.prototype.findIndex() returns the index of the first array element whose testing function returns a truthy value. It returns -1 when no element matches.

How it works

Call findIndex() with a testing function. JavaScript invokes that function for array elements in ascending index order and stops as soon as it returns a truthy value, returning that element’s index; when no element matches, the result is -1.

Syntax

findIndex(callbackFn)
findIndex(callbackFn, thisArg)

Parameters

NameTypeDescription
callbackFnFunctionA function invoked for each element. It should return a truthy value for a match and a falsy value otherwise; it receives element, index, and array.
thisArganyOptional value used as this when invoking callbackFn.

Returns

The index of the first element that passes the test; otherwise, -1.

Key points

  • It identifies the first matching element by position rather than returning the element itself.
  • Evaluation follows increasing numeric indexes.
  • The operation short-circuits at the first truthy test result.
  • The predicate can inspect the current value.
  • The predicate can make its decision using the current position.
  • The callback can access the complete receiver array.
  • It can operate on suitable array-like objects, not only actual Array instances.

Common mistakes

  • A non-match is represented by -1, so do not treat the returned number as a valid index without checking it.

Example: Find the first matching index

const inventory = [
  { id: "a1", stock: 0 },
  { id: "b2", stock: 4 },
  { id: "c3", stock: 9 },
];

const index = inventory.findIndex((item) => item.stock > 0);
console.log(index); // 1

The callback tests each inventory item in order; the method returns the index of the first item with stock available.

When to use it

Find a matching object index

Locate a record by a condition while retaining its position for replacement or removal.

Search by computed criteria

Determine whether an ordered collection contains an item satisfying a predicate while preserving its numeric location.

Process array-like data

Inspect array-like structures that expose length and integer-keyed properties.

Interview questions

QWhat does Array.prototype.findIndex() return?

It returns the index of the first array element whose test callback produces a truthy value. The search proceeds from the lowest index upward and stops immediately after the first match.

Follow-up: How does it differ from find()? find() returns the matching element itself, whereas findIndex() returns its numeric position.

QWhat arguments does the callback receive?

The callback receives the current element, its index, and the array on which findIndex() was called. A truthy callback result indicates a match; a falsy result continues the search.

Follow-up: How can callback context be supplied? Pass a second argument as thisArg; it becomes the callback's this value when the callback is invoked.

QWhat happens when no element satisfies the predicate?

If no callback invocation returns a truthy value, the method returns -1. This distinguishes “not found” from a valid zero-based index, including index 0.

Follow-up: Can it be used on array-like objects? Yes: the method is generic and only requires a length property plus integer-keyed properties.

  • Array.prototype.find()
  • Array.prototype.indexOf()
  • TypedArray.prototype.findIndex()

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