The find() method returns the value of the first array element that satisfies the provided test function.
Example
find() Syntax
The syntax of the find() method is:
arr.find(callback(element, index, arr),thisArg)
Here, arr is an array.
find() Parameters
The find() method takes in:
- callback - Function to execute on each element of the array. It takes in:
- element - The current element of array.
- thisArg (optional) - Object to use as
thisinside callback.
find() Return Value
- Returns the value of the first element in the array that satisfies the given function.
- Returns undefined if none of the elements satisfy the function.
Example 1: Using find() method
Output
8 1
Example 2: find() with Object elements
Output
{ name: 'Alan', age: 20 }
{ name: 'Alan', age: 20 }
Recommended Reading: JavaScript Array.findIndex()