JavaScript findIndex: array findIndex and objects by property

findIndex javascript and js findIndex: predicate scan with Array.prototype.findIndex, return index or -1, optional thisArg, javascript find index of object in array by property, and how findIndex differs from find and indexOf. Runnable snippets; short notes describe console output.

Published

Updated

Read time 4 min read

Reviewed byDeepak Prasad

JavaScript findIndex: array findIndex and objects by property

Searches bundle the same API under many spellings: findindex javascript, javascript findindex, findindex js, findindex in js, or simply array findindex. All of them point to Array.prototype.findIndex: a method that walks an array from the beginning, runs your test function on each element, and returns the index of the first match—or -1 when nothing satisfies the predicate.

That is different from find, which returns the element itself, and from indexOf, which looks for a specific value with SameValueZero comparison rather than an arbitrary predicate. For anything beyond “equals this primitive,” findindex in javascript is usually the right tool—including the common pattern javascript find index of object in array by property.

Tested on: Node.js v20.18.2. A short note after each snippet describes what you should see in the console.


Quick reference

Use findIndex when you need the index of the first element that matches a custom test; use find for the value, indexOf / includes for equality to a known value.

Goal API
First index matching a predicate arr.findIndex(fn) → index or -1
First matching element arr.find(fn) → element or undefined
Index of a known value arr.indexOf(value)

findIndex method in javascript: signature and return value

The callback receives (element, index, array) and should return a truthy value when the element matches. The method returns a non‑negative integer index on success, or -1 if the callback never returned truthy before the end of the array. An optional second argument sets this inside a non‑arrow callback.


Numeric array: first index where a condition holds

Here the predicate keeps scanning until it sees a value strictly greater than 10. Values at indexes 0 and 1 fail; index 2 is the first pass.

javascript
const numbers = [5, 10, 15, 20, 25];
const index = numbers.findIndex((element) => element > 10);
console.log(index);
Output

You should see one line: 2.


javascript find index of object in array by property

Objects are compared like any other element: read a field inside the callback. This answers the long-tail query about locating a record by name, id, or similar keys.

javascript
const people = [
  { name: 'Alice' },
  { name: 'Bob' },
  { name: 'John' },
  { name: 'Sue' },
];
const index = people.findIndex((el) => el.name === 'John');
console.log(index);
Output

You should see one line: 2.


When nothing matches

If the predicate is never satisfied, the return value is always -1, which makes an easy branch for “not found” logic.

javascript
const nums = [1, 2, 3];
console.log(nums.findIndex((n) => n > 10));
Output

You should see one line: -1.


Optional thisArg (and why arrows change the story)

The second parameter binds this for a traditional function callback. In real code, prefer closing over outer variables instead of thisArg unless you are mirroring library patterns.

javascript
const ids = [10, 20, 30];
const target = { min: 25 };
const i = ids.findIndex(function (n) {
  return n >= this.min;
}, target);
console.log(i);
Output

You should see one line: 2.

If you switch the callback to an arrow function, it ignores thisArg because arrows close over lexical scope—keep that in mind when refactoring.


findIndex vs find vs indexOf (short comparison)

  • findIndex → index or -1, predicate-based.
  • find → element or undefined, same predicate idea.
  • indexOf / includes → value search with SameValueZero, not a free-form predicate.

Summary

findIndex is the predicate-driven Array.prototype.findIndex lookup: it scans until your callback returns truthy, then yields that index or -1 when nothing matches. People compare it to indexOf, but indexOf searches for a concrete value with SameValueZero, while findIndex searches with arbitrary logic—ranges, substring tests, or nested object fields.

Because -1 is a valid array index for some algorithms but a sentinel here, the usual FAQ is “how do I branch?”—check idx === -1 before using the index, and switch to find when you need the element instead of its position. Remember arrow callbacks ignore the thisArg parameter that findIndex still accepts for ordinary functions.


Reference

Olorunfemi Akinlua

Boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs …