Array.prototype.findIndex
Returns the index of the first element in an array that satisfies the provided testing function
Syntax
array.findIndex(callbackFn, thisArg?)Parameters
| Parameter | Type | Description |
|---|---|---|
| callbackFn | (element, index, array) => boolean | Function to test each element |
| thisArg | any | Value to use as this when executing callbackFn |
Return Value
The index of the first matching element, or -1 if no element matches
Examples
const numbers = [5, 12, 8, 130];
const idx = numbers.findIndex(n => n > 10);
console.log(idx); // 1const users = [
{ name: 'Alice', active: false },
{ name: 'Bob', active: true }
];
const activeIdx = users.findIndex(u => u.active);
console.log(activeIdx); // 1const letters = ['a', 'b', 'c', 'd'];
const idx = letters.findIndex(l => l === 'z');
console.log(idx); // -1Understanding Array.prototype.findIndex
The Array.prototype.findIndex method in JavaScript returns the index of the first element in an array that satisfies the provided testing function. It belongs to the Array object and is one of the most widely used methods for working with array values in modern JavaScript and TypeScript applications.
The method signature is array.findIndex(callbackFn, thisArg?). It accepts 2 parameters: callbackFn, thisArg. When called, it returns the index of the first matching element, or -1 if no element matches. Understanding when and how to use findIndex() helps you write more expressive, readable code.
Common use cases for Array.prototype.findIndex include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like array-find, array-indexof, array-findlastindex, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Array.prototype.findIndex is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
Array.prototype.findReturns the first element in the provided array that satisfies the provided testing function
Array.prototype.indexOfReturns the first index at which a given element can be found in the array, or -1 if it is not present
Array.prototype.findLastIndexIterates the array in reverse order and returns the index of the first element that satisfies the provided testing function
Array.prototype.includesDetermines whether an array includes a certain value among its entries, returning true or false
More Array Methods
Other methods in the Array object
Related Tools
More Array Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.