Array.prototype.findIndex()
findIndex(callbackFn, thisArg) Returns:
number · Added in vES6 · Updated March 13, 2026 · Array Methods array find search index
findIndex() iterates through an array and returns the index of the first element that passes the test function. If no element passes, it returns -1. Unlike find() which returns the element itself, findIndex() gives you the position in the array.
Syntax
arr.findIndex(callbackFn, thisArg)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
callbackFn | function | — | Function to test each element |
element | any | — | Current element being processed |
index | number | — | Index of current element |
array | array | — | Array that findIndex was called on |
thisArg | object | undefined | Value to use as this when executing callbackFn |
The callback function should return true for elements you want to find.
Examples
Finding a user position
const users = [
{ name: "Alice", role: "admin" },
{ name: "Bob", role: "editor" },
{ name: "Charlie", role: "admin" }
];
const firstAdmin = users.findIndex(user => user.role === "admin");
console.log(firstAdmin);
// 0 (Alice is at index 0)
// If not found, returns -1
const notFound = users.findIndex(user => user.role === "owner");
console.log(notFound);
// -1
Finding the first even number
const numbers = [1, 3, 5, 8, 9, 10];
const firstEven = numbers.findIndex(n => n % 2 === 0);
console.log(firstEven);
// 3 (the number 8 at index 3)
Using thisArg
const threshold = { min: 10 };
const index = [5, 8, 12, 3].findIndex(function(item) {
return item > this.min;
}, threshold);
console.log(index);
// 2 (12 is the first value greater than 10)
Common Patterns
Finding and updating an element
const items = [
{ id: 1, name: "Widget", price: 29.99 },
{ id: 2, name: "Gadget", price: 49.99 }
];
// Find the index, then update
const idx = items.findIndex(item => item.id === 2);
if (idx !== -1) {
items[idx] = { ...items[idx], price: 39.99 };
}
console.log(items[1].price);
// 39.99
Chaining with other array methods
const products = [
{ name: "Laptop", inStock: false, price: 999 },
{ name: "Mouse", inStock: true, price: 29 },
{ name: "Keyboard", inStock: true, price: 79 }
];
// Find first in-stock item, then get its price
const firstInStockIdx = products.findIndex(p => p.inStock);
const price = products[firstInStockIdx]?.price;
console.log(price);
// 29
When to Use
- When you need the position of an element rather than the element itself
- When you will use the index for further operations (splicing, updating)
- When searching large arrays where finding the position is more efficient than searching twice
When Not to Use
- Use
find()if you need the element, not the index - Use
indexOf()for simple value equality checks (faster) - Use
includes()if you only need to know whether an element exists
See Also
- array::find — returns the element
- array::indexOf — uses strict equality
- array::filter — returns all matches