JavaScript Array findIndex() Method

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Index search

What You’ll Learn

The findIndex() method returns the index of the first element that passes your test, or -1 if none match. It pairs naturally with splice and bracket updates. This tutorial covers syntax, five examples, and comparisons with find() and indexOf().

01

Syntax

findIndex(cb)

02

Index

0, 1, 2…

03

No match

-1

04

Stops early

First hit

05

Objects OK

Custom test

06

ES2015

Modern support

Introduction

Sometimes you need to know where an item lives, not just what it is. Maybe you will update that slot, remove it with splice, or scroll a list to the matching row. findIndex() gives you that position.

It mirrors find() in search logic but returns a number instead of the element. When nothing matches, you get -1—the same convention as indexOf.

Understanding the findIndex() Method

array.findIndex(callback) walks the array from index 0 upward. For each element it runs your callback with (element, index, array). The first truthy result stops the search and returns that index.

If every callback is falsy, the method returns -1.

💡
Beginner Tip

When you need the value, use find(). When you need the position—for update, delete, or UI focus—use findIndex().

📝 Syntax

General form of Array.prototype.findIndex:

JavaScript
array.findIndex(callback(element, index, array), thisArg)

Parameters

  • callback — predicate tested on each element; first truthy match wins.
  • element — current array item.
  • index — optional; position of the element (also usable inside the test).
  • array — optional; the array being searched.
  • thisArg — optional; value to use as this inside the callback.

Return value

  • Index of the first matching element (0-based).
  • -1 if no element passes the test.
  • Original array unchanged by findIndex() itself.

Common patterns

  • arr.findIndex(n => n > 25) — first value above 25.
  • items.findIndex(i => i.id === id) — lookup by id.
  • if (i !== -1) arr.splice(i, 1) — remove first match.
  • if (i !== -1) arr[i] = updated — replace in place.

⚡ Quick Reference

GoalCode
First match indexarr.findIndex(callback)
First > 25arr.findIndex(n => n > 25)
No match-1
Stops early?Yes
Return typeNumber

📋 findIndex() vs find() vs indexOf() vs findLastIndex()

Same family of searches, different return types and search directions.

findIndex
first index

Number or -1

find
first value

Element or undefined

indexOf
=== value

Strict equality

findLastIndex
last index

Tail-first (ES2023)

Examples Gallery

Open DevTools Console (F12) or use Try-it labs. Example N maps to ?tryit=N.

📚 Getting Started

Locate the first index that passes a test.

Example 1 — Index of the First Value Greater Than 25

Return 2 because 30 at index 2 is the first match.

JavaScript
const numbers = [10, 20, 30, 40, 50];

const index = numbers.findIndex((num) => num > 25);

console.log(index);
// 2
Try It Yourself

How It Works

Indexes 0 and 1 fail the test. Index 2 (30) passes, so findIndex returns 2 and stops.

Example 2 — Handle -1 When Nothing Matches

Always check for -1 before using the index in updates or splices.

JavaScript
const numbers = [10, 20, 30, 40, 50];

const index = numbers.findIndex((num) => num > 100);

if (index === -1) {
  console.log("No element greater than 100 found.");
} else {
  console.log("Found at index:", index);
}
// No element greater than 100 found.
Try It Yourself

How It Works

-1 is a valid number, so a loose check like if (index) is wrong—index 0 would fail. Compare explicitly with === -1.

📈 Practical Patterns

Index-aware tests, objects, and in-place updates.

Example 3 — Use the Index Parameter in the Callback

Find the first element whose array index is even.

JavaScript
const numbers = [10, 20, 30, 40, 50];

const evenIndex = numbers.findIndex((_num, index) => index % 2 === 0);

console.log(evenIndex);
// 0
Try It Yourself

How It Works

The second callback argument is the index. Here the value is ignored (_num); only position matters. Index 0 is even, so the result is 0.

Example 4 — Find a User Object by Name

Search an array of objects and get Bob’s index.

JavaScript
const users = [
  { id: 1, name: "Alice" },
  { id: 2, name: "Bob" },
  { id: 3, name: "Charlie" }
];

const index = users.findIndex((user) => user.name === "Bob");

console.log(index);
// 1
Try It Yourself

How It Works

indexOf("Bob") cannot search object properties. A callback comparing user.name is the correct approach.

Example 5 — Update an Element at the Found Index

Locate a todo by id, then replace it in place.

JavaScript
const todos = [
  { id: 1, text: "Buy milk", done: false },
  { id: 2, text: "Walk dog", done: false }
];

const i = todos.findIndex((t) => t.id === 2);

if (i !== -1) {
  todos[i] = { ...todos[i], done: true };
}

console.log(todos[1]);
// { id: 2, text: "Walk dog", done: true }
Try It Yourself

How It Works

This is a classic reason to choose findIndex over find: you get a numeric slot for assignment or splice without scanning the array twice.

🚀 Common Use Cases

  • In-place updates — find index, then assign or splice.
  • UI selection — highlight the row at the returned index.
  • Validation — locate the first failing field in an array.
  • Duplicate checks — find if another item shares a key.
  • Queue processing — find the first pending job index.
  • Immutable patterns — combine with spread to replace one slot immutably.

🧠 How findIndex() Runs

1

Start at 0

Iterate indexes in ascending order.

Loop
2

Run callback

Test (element, index, array) at each step.

Predicate
3

Truthy?

Return current index and stop immediately.

Match
4

End of array

Return -1 if no element passed.

📝 Notes

  • Returns -1 on failure—same as indexOf, unlike find() which returns undefined.
  • Index 0 is valid; never use if (index) as a found check.
  • Short-circuits on the first match; later duplicates are ignored.
  • Holey arrays may still invoke the callback with undefined at hole indexes.
  • For the last match, use findLastIndex() (ES2023).
  • ES2015 method—polyfill needed for Internet Explorer.

Browser & Runtime Support

Array.prototype.findIndex() was added in ES2015 (ES6) alongside find(). It is available in all evergreen browsers and modern Node.js versions.

Baseline · ES2015

Array.prototype.findIndex()

Supported in Chrome 45+, Firefox 25+, Safari 7.1+, Edge 12+, and Node 4+. Not available in Internet Explorer.

97% Modern browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
Array.findIndex() Excellent

Bottom line: Safe for modern web apps. For IE11, use a polyfill or a manual loop that returns -1 when no match is found.

Conclusion

The findIndex() method bridges search and mutation: you test with a flexible callback and get a numeric position ready for update, delete, or UI focus.

Pair it with explicit -1 checks, and choose find() when you only need the value itself.

💡 Best Practices

✅ Do

  • Compare with index === -1 for not found
  • Use findIndex before splice or bracket assignment
  • Write clear predicates for object lookups
  • Prefer findIndex over indexOf for custom tests
  • Handle index 0 as a successful match

❌ Don’t

  • Use truthiness on the index (if (index) breaks at 0)
  • Expect the element—use find() for values
  • Assume -1 and undefined mean the same thing
  • Mutate the array unpredictably during the search
  • Use findIndex when you need every match (filter)

Key Takeaways

Knowledge Unlocked

Five things to remember about Array.findIndex()

Your foundation for position-based array searches.

5
Core concepts
🔢 02

Returns index

Number.

Position
03

No match

-1

Sentinel
04

First only

Stops early.

Fast
🔄 05

vs find()

Index vs value.

Compare

❓ Frequently Asked Questions

findIndex() returns the index of the first element for which the callback returns a truthy value. If no element matches, it returns -1.
No. findIndex() only reads elements through your callback. The original array is unchanged unless you mutate it afterward using the returned index.
It returns -1, the same sentinel value used by indexOf and lastIndexOf when a search fails.
findIndex() returns the numeric position (or -1). find() returns the matching element itself (or undefined). Use findIndex when you need to update, remove, or reference a slot by position.
indexOf searches for strict equality (===) with a value. findIndex() accepts any callback predicate, which is essential for objects and complex conditions.
findIndex() is an ES2015 (ES6) feature. All modern browsers support it; Internet Explorer does not.
Did you know?

Index 0 is falsy in JavaScript, so if (arr.findIndex(...)) wrongly treats a match at the start as “not found.” Always compare explicitly: index !== -1.

Continue to findLast()

Learn how to search from the end of an array for the last matching element.

findLast() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful