JavaScript find()
The find()
method is an array method in JavaScript that returns the value of the first element in an array that satisfies a given condition. It is similar to the filter()
method but the find()
method returns only the first match.
Syntax
array.find(function(currentValue, index, arr),thisValue)
function(currentValue, index, arr)
: This parameter specifies the function to be executed on each value in the array.currentValue
: The value of the current element being processed in the array.index
: Optional parameter that specifies the index of the current element being processed in the array.arr
: Optional parameter that specifies the array that thefind()
method is being called on.thisValue
: Optional parameter. A value to be passed to the function to be used as its "this" value.
Example
let numbers = [2, 5, 7, 9];
let result = numbers.find(function(number){
return number > 5;
});
console.log(result); // Output: 7
Output
7