Return the first element that satisfies the provided testing function.
The find() method returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.
The callback function is called for each element in the array until it returns a truthy value. find() then immediately returns that element without checking the remaining elements, making it efficient for searches.
find() was introduced in ES2015 and is the preferred way to locate an element in an array. For finding the index instead of the value, use findIndex(). For checking existence only, use includes() or some().
| Name | Description | Optional |
|---|---|---|
callbackFn |
A function to execute for each element, returning true when the desired element is found. | No |
thisArg |
Value to use as this when executing callbackFn. | Yes |
const users = [{name: 'Alice'}, {name: 'Bob'}];
users.find(u => u.name === 'Bob'); // {name: 'Bob'}