Open In App

What is find() method in JavaScript ?

In JavaScript, the find() method is an array method used to retrieve the value of the first element in an array that satisfies a provided testing function. It iterates over each element in the array and returns the value of the first element for which the testing function returns true. If no such element is found, undefined is returned.

Syntax:

arr.find(function(element, index, array), thisValue);

Parameters:

Example: Here, we have an array numbers containing numeric values. We use the find() method to search for the first element greater than 25. The callback function checks if each element is greater than 25. The first element that satisfies this condition is equal to 30, so find() returns 30 as the output.




const numbers = [10, 20, 30, 40, 50];
 
const foundValue = numbers.find(element => element > 25);
 
console.log(foundValue);

Output
30
Article Tags :