The findIndex()
method returns the index of the first element in an array that satisfies the provided testing function. If no elements satisfy the testing function, -1 is returned.
The findIndex()
method returns the index of the first element in an array that satisfies the provided testing function. If no elements satisfy the testing function, -1 is returned.
See also the find()
method, which returns the first element that satisfies the testing function (rather than its index).
// Arrow function findIndex((element) => { /* … */ } ) findIndex((element, index) => { /* … */ } ) findIndex((element, index, array) => { /* … */ } ) // Callback function findIndex(callbackFn) findIndex(callbackFn, thisArg) // Inline callback function findIndex(function(element) { /* … */ }) findIndex(function(element, index) { /* … */ }) findIndex(function(element, index, array){ /* … */ }) findIndex(function(element, index, array) { /* … */ }, thisArg)
callbackFn
A function used to test elements in the array.
The function is called with the following arguments:
element
The current element being processed in the array.
index
The index of the current element being processed in the array.
array
The array findIndex()
was called upon.
The callback must return a truthy value to indicate an appropriate element has been found. The index of this element is then returned by findIndex()
.
thisArg
Optional
Optional object to use as this
when executing callbackFn
.
The index of the first element in the array that passes the test. Otherwise, -1
.
The findIndex()
method executes the callbackFn
function once for every index in the array, in ascending order, until it finds the one where callbackFn
returns a truthy value.
If such an element is found, findIndex()
immediately returns the element's index. If callbackFn
never returns a truthy value (or the array's length
is 0
), findIndex()
returns -1
.
Note: Unlike other array methods such as some()
, callbackFn
is run even for indexes with unassigned values.
callbackFn
is invoked with three arguments:
If a thisArg
parameter is passed to findIndex()
, it will be used as the this
inside each invocation of the callbackFn
. If it is not provided, then undefined
is used.
The range of elements processed by findIndex()
is set before the first invocation of callbackFn
. Elements which are assigned to indexes already visited, or to indexes outside the range, will not be visited by callbackFn
. callbackFn
will not process the elements appended to the array after the call to findIndex()
begins. If an existing, unvisited element of the array is changed by callbackFn
, its value passed to the callbackFn
will be the value at the time findIndex()
visits the element's index. Elements that are deleted
are still visited.
Warning: Concurrent modification of the kind described in the previous paragraph frequently leads to hard-to-understand code and is generally to be avoided (except in special cases).
The following example returns the index of the first element in the array that is a prime number, or -1
if there is no prime number.
function isPrime(element) { if (element % 2 === 0 || element < 2) { return false; } for (let factor = 3; factor <= Math.sqrt(element); factor += 2) { if (element % factor === 0) { return false; } } return true; } console.log([4, 6, 8, 9, 12].findIndex(isPrime)); // -1, not found console.log([4, 6, 7, 9, 12].findIndex(isPrime)); // 2 (array[2] is 7)
The following example finds the index of a fruit using an arrow function:
const fruits = ["apple", "banana", "cantaloupe", "blueberries", "grapefruit"]; const index = fruits.findIndex((fruit) => fruit === "blueberries"); console.log(index); // 3 console.log(fruits[index]); // blueberries
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | Deno | Node.js | |
findIndex |
45 |
12 |
25 |
No |
32 |
8 |
45 |
45 |
4 |
32 |
8 |
5.0 |
1.0 |
4.0.0
0.12.0
|
© 2005–2022 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex