Array.findIndex()
Returns the index of the first match, or -1 if not found.
array.findIndex(predicateFn, thisArg?)This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Returns the index of the first match, or -1 if not found.
The findIndex() method executes a predicate function for each array element until it finds one where the function returns a truthy value. It then returns the index of that element immediately, short-circuiting the remaining iterations. If the predicate never returns a truthy value, findIndex() returns -1. In terms of performance, it has a linear time complexity of O(n). One notable edge case is its behavior with sparse arrays: unlike methods like forEach() or map(), findIndex() visits every index from 0 to length - 1, including 'holes', passing undefined to the predicate for empty slots. When performing searches involving dates, it is recommended to compare ISO strings or use getTime() to compare numeric timestamps for reliable results. IE not supported.
Quick reference
Syntax
array.findIndex(predicateFn, thisArg?)
Inputs
Parameters
See it in practice
Examples
Finding the index of an object by property
var users = [
{ id: 10, name: 'Alice' },
{ id: 25, name: 'Bob' },
{ id: 32, name: 'Charlie' }
];
var index = users.findIndex(function(user) {
return user.id === 25;
});
console.log(index);1
Locates the first user object where the id matches 25 and returns its position.
Searching with Date comparisons
var events = [
{ name: 'Meeting', date: '2023-01-10T10:00:00Z' },
{ name: 'Workshop', date: '2023-05-20T14:00:00Z' }
];
var targetDate = new Date('2023-05-20T14:00:00Z').getTime();
var index = events.findIndex(function(event) {
return new Date(event.date).getTime() === targetDate;
});
console.log(index);1
Uses getTime() to ensure a reliable numeric comparison between date objects.
Handling missing elements
var numbers = [5, 12, 8, 130, 44];
var index = numbers.findIndex(function(element) {
return element > 200;
});
console.log(index);-1
Returns -1 when the predicate function never returns a truthy value.
Debug faster
Common Errors
LogicError
Cause: Using the returned index directly in a truthiness check. If the matching element is at index 0, the check will evaluate to false because 0 is falsy.
Fix: Always compare the result of findIndex() to -1 to verify if a match was found.
if (arr.findIndex(predicate)) { /* This fails if index is 0 */ }TypeError
Cause: Forgetting to return a value from the predicate function. If nothing is returned, the predicate evaluates to undefined (falsy), and findIndex will return -1.
Fix: Ensure the predicate function includes a return statement or uses an implicit arrow function return.
arr.findIndex(function(item) { item === target; }); // Missing 'return'Runtime support
Compatibility
Source: MDN Web Docs
Common questions
Frequently Asked Questions
Returns the index of the first match, or -1 if not found.
predicate: Returns true for the desired element. thisArg: Value used as 'this' inside predicate.
LogicError: Always compare the result of findIndex() to -1 to verify if a match was found. TypeError: Ensure the predicate function includes a return statement or uses an implicit arrow function return.