JavaScript Reference
Array.prototype.findIndex()
Returns the index of the first element that matches a test. Returns -1 if nothing matches.
Last updated: October 2026
Definition: Array.prototype.findIndex() returns the index of the first element that passes the provided test function. If no element matches, it returns -1. It short-circuits on the first match — perfect for locating an item's position before updating or removing it.
🎬 Live Example
Five common findIndex patterns. Click any button to see the output.
💡 findIndex returns a number. -1 means "not found".
Basic Syntax
array.findIndex(callback(element, index, array))
// Returns the index of the first match, or -1
Same callback signature as find — but you get the position, not the element.
🎯 Syntax & Parameters
| Parameter | Type | Description |
|---|---|---|
callback | Function | Runs on each element until it returns truthy. |
element | any | The current element. |
index | number | Index of the current element. |
array | Array | The original array. |
Find Index in an Array of Objects
const users = [
{ id: 10, name: 'Alice' },
{ id: 20, name: 'Bob' },
{ id: 30, name: 'Carol' }
];
const index = users.findIndex(u => u.id === 20);
// 1
if (index !== -1) {
console.log('Found at index', index);
} else {
console.log('Not found');
}
Always check !== -1 before using the index.
Update an Element by Index
const products = [
{ sku: 'A1', name: 'Widget' },
{ sku: 'B2', name: 'Gadget' },
{ sku: 'C3', name: 'Doohickey' }
];
// Find the index of the item to update
const idx = products.findIndex(p => p.sku === 'B2');
if (idx !== -1) {
products[idx].name = 'Updated Gadget';
}
// products[1].name is now 'Updated Gadget'
Remove an Element by Index
const fruits = ['apple', 'banana', 'cherry', 'date'];
const idx = fruits.findIndex(f => f === 'cherry');
if (idx !== -1) {
fruits.splice(idx, 1);
}
// fruits is now ['apple', 'banana', 'date']
The classic remove pattern: findIndex + splice. Safe, because we check for -1 first.
findIndex on Primitives
const numbers = [10, 20, 30, 40, 50];
// Find index of specific value (use indexOf for this)
numbers.findIndex(n => n === 30); // 2
numbers.indexOf(30); // 2
// Complex test — that's what findIndex is for
numbers.findIndex(n => n > 25 && n < 45); // 2 (matches 30)
💡 Rule: Use indexOf when looking for a specific primitive value. Use findIndex when the test is complex — like checking a property on an object.
Real-World: Check for Duplicates Before Adding
const cart = [
{ id: 1, name: 'Widget', qty: 1 },
{ id: 2, name: 'Gadget', qty: 3 }
];
function addToCart(item) {
const existingIndex = cart.findIndex(c => c.id === item.id);
if (existingIndex !== -1) {
cart[existingIndex].qty += 1;
} else {
cart.push({ ...item, qty: 1 });
}
}
addToCart({ id: 1, name: 'Widget' });
// cart[0].qty is now 2
🎯 findIndex vs find vs indexOf
| Method | Returns | Use when |
|---|---|---|
find | Element or undefined | You need the element itself |
findIndex | Index or -1 | You need the position (update, remove) |
indexOf | Index or -1 | You're looking for a specific primitive value |
lastIndexOf | Index or -1 | You need the last occurrence of a primitive |
⚠️ Common Mistakes
- Forgetting -1. findIndex returns -1 for no match, not undefined or null. Check
index !== -1before using it. - Using -1 as a valid index.
arr[-1]is undefined, not the last element. Guard against -1 first. - Confusing findIndex with indexOf. indexOf compares values with
===and can't take a callback. Use findIndex for objects. - Using indexOf for objects. It won't work — objects are compared by reference. Use findIndex.
- Using findIndex when only existence matters. Use
some()— it returns a boolean and is more expressive. - Not handling duplicates. findIndex returns the first match only. If you need all matches, use filter with indices.
- Mutating while iterating. Avoid modifying the array inside the findIndex callback.
❓ Frequently Asked Questions
What does Array.findIndex() do in JavaScript?
Array.findIndex() returns the index of the first element in the array that passes a test function. If no element matches, it returns -1. It short-circuits as soon as a match is found.
What is the difference between findIndex and indexOf?
findIndex accepts a callback function for complex tests. indexOf accepts a specific value and uses strict equality. Use findIndex for object properties and custom logic; use indexOf for primitive values like numbers and strings.
What is the difference between find and findIndex?
find returns the matching element. findIndex returns the index of the matching element. Both short-circuit at the first match. find returns undefined when no match is found, findIndex returns -1.
Can findIndex return multiple indexes?
No. findIndex returns only the first matching index. For all matching indexes, use a loop or filter + map with index tracking.
Does findIndex modify the original array?
No. findIndex does not modify the array. It only reads. Its return value is a number (the index or -1).