Master Code
On The Go

Learn. Practice. Build.

Home › Code Snippets › JavaScript › Reference › findIndex()

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.

Input:

                
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
callbackFunctionRuns on each element until it returns truthy.
elementanyThe current element.
indexnumberIndex of the current element.
arrayArrayThe original array.
1

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.

2

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'
3

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.

4

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.

5

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
findElement or undefinedYou need the element itself
findIndexIndex or -1You need the position (update, remove)
indexOfIndex or -1You're looking for a specific primitive value
lastIndexOfIndex or -1You need the last occurrence of a primitive

⚠️ Common Mistakes

❓ 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).

🎯 What's Next?

← All JavaScript Reference