Array.prototype.find()
Returns the first element that satisfies the provided testing function.
Syntax
arr.find(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
any — The first matching element, or undefined if none match.
Examples
const nums = [5, 12, 8, 130, 44];
console.log(nums.find(n => n > 10));
Output
12
const users = [{ id: 1 }, { id: 2 }];
console.log(users.find(u => u.id === 2));
Output
{ id: 2 }
Notes
Returns undefined (not -1) when nothing matches. Use `findIndex` to get the
position instead of the value. Does not mutate the array.
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | 45 |
| firefox | 25 |
| safari | 8.0 |
| edge | 12 |
| node | 4.0 |