findIndex Method (Array) (JavaScript)
Returns an index value for the first array element that meets test criteria specified in a callback function.
Syntax
arrayObj.findIndex(callbackfn [, thisArg]);
Parameters
arrayObj
Required. The array object.
callbackfn
Required. The callback function to test each element in the array.
thisArg
Optional. Specifies the this
object in the callback function. If not specified, the this
object is undefined.
Remarks
The findIndex
calls the callback function one time for each element in the array, in ascending order, until an element returns true
. As soon as an element returns true, findIndex
returns the index value of the element that returns true. If no elements in the array return true, findIndex
returns -1.
findIndex
does not mutate the array object.
Callback Function Syntax
The syntax of the callback function is as follows:
function callbackfn(value, index, thisArg)
You can declare the callback function by using up to three parameters.
The callback function parameters are as follows.
Callback argument | Definition |
---|---|
value |
The value of the array element. |
index |
The numeric index of the array element. |
arrayObj |
The array object to be traversed. |
Example
In the following example, the callback function tests whether each element in the array is equal to 2.
[1,2,3].findIndex(function(x) { x == 2; });
// Returns an index value of 1.
Example
The following example uses arrow syntax to test each element. In this example, no elements return true
, and findIndex
returns -1.
[1,2,3].findIndex(x => x == 4);
// Returns an index value of -1.
Requirements
Supported in Microsoft Edge (Edge browser). Also supported in Store apps (Microsoft Edge on Windows 10). See Version Information.
Not supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards, Internet Explorer 10 standards, Internet Explorer 11 standards. Not supported in Windows 8.1.