Array.prototype.findIndex()

Returns the index of the first element passing the test, or -1 if none does.

Signature
findIndex(callbackFn)
findIndex(callbackFn, thisArg)
  • Added in JavaScript ES2015

Parameters

ParameterTypeDescription
$callbackFnFunctionFunction run for each element, called with (element, index, array). Return a truthy value to signal a match.
$thisArgoptionalany= undefinedValue to use as this when executing callbackFn. Ignored by arrow functions.

Return value

numberThe index of the first element for which callbackFn returned a truthy value, or -1 if no element matched.

Errors

Nothing of its own — errors thrown by callbackFn propagate and stop the search.

findIndex() is find() that hands back a position instead of a value. It scans from index 0, stops at the first truthy callback result, and returns that index — or -1 when nothing matched.

When to reach for it

Whenever the position is what you actually need: to splice() an element out, to overwrite arr[i] in place, to slice around a marker, or simply to test existence without caring about the value. For a plain equality check on primitives, indexOf() is shorter; findIndex() is the one that takes a predicate, so it can match on object fields and on NaN.

The -1 trap

Because -1 is a number, it flows silently into arithmetic and indexing. arr[findIndex(...)] gives undefined on a miss, but arr.at(findIndex(...)) returns the last element, and splice(-1, 1) deletes it. Always compare against -1 before using the result. Note too that if (idx) is wrong twice over: it is falsy for a valid index of 0 and truthy for -1.

Unlike filter(), findIndex() visits holes in sparse arrays and treats them as undefined.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Find where to splice an item out of a list by id
  • Replace an element in place after locating it by predicate
  • Slice an array at the first row that fails validation
  • Locate NaN in a numeric array, which indexOf() cannot do

Frequently asked questions

What does findIndex() return if nothing matches?

-1. Always compare with !== -1 rather than testing truthiness, since a valid match at index 0 is falsy and -1 is truthy.

What is the difference between findIndex() and indexOf()?

indexOf() takes a value and compares with strict equality; findIndex() takes a predicate function. That makes findIndex() the only option for object fields and for locating NaN, which never equals itself.

How do I get both the element and its index?

Call findIndex() once and read arr[idx] after the -1 check, or use entries(): for (const [i, item] of arr.entries()). Calling find() and findIndex() separately scans the array twice.

More examples

Removing an item by id

Prints [{ id: 1 }, { id: 3 }]. The idx !== -1 guard is essential: without it, a missing id would call splice(-1, 1) and delete the last todo instead of nothing.

Example · javascript
const todos = [{ id: 1 }, { id: 2 }, { id: 3 }];

const idx = todos.findIndex((t) => t.id === 2);
if (idx !== -1) {
  todos.splice(idx, 1);
}

console.log(todos);

Why -1 is dangerous

Prints -1, then undefined, then 30. Bracket access on -1 is harmlessly undefined, but at() interprets -1 as counting back from the end and cheerfully returns the last element — a bug that looks like a successful match.

Example · javascript
const arr = [10, 20, 30];
const miss = arr.findIndex((n) => n > 100);

console.log(miss);
console.log(arr[miss]);
console.log(arr.at(miss));

Discussion

  • Be the first to comment on this lesson.