Array.prototype.findIndex()
Returns the index of the first element that satisfies the testing function.
Syntax
arr.findIndex(callback, thisArg) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
callback | Function | Yes | Predicate called with (element, index, array). Return truthy to select the element. |
thisArg | any | No | Value to use as `this` when executing the callback. |
Returns
number — The index of the first matching element, or -1 if none match.
Examples
const nums = [5, 12, 8, 130, 44];
console.log(nums.findIndex(n => n > 10));
Output
1
console.log([1, 2, 3].findIndex(n => n > 9));
Output
-1
Notes
Returns -1 when nothing matches. Use `find` to get the value instead of the
index, or `indexOf` for a strict equality search.
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | 45 |
| firefox | 25 |
| safari | 8.0 |
| edge | 12 |
| node | 4.0 |