Open In App

What is find() method in JavaScript ?

Last Updated : 15 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • array: This is the array on which the .filter() function was called. It is an optional parameter.
  • index: This is the index of the current element being processed by the function. It is an optional parameter.
  • element: This is the current element being processed by the function.

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.

Javascript




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


Output

30

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads