Open In App

JavaScript Array find() Method

JavaScript find() method is used to retrieve the first element in an array that satisfies a specified condition.

Array find() method executes a provided function once for each element in the array until it finds one where the function returns a truthy value. The find() method does not change the original array.



Array find() Method Syntax:

array.find(function(currentValue, index, arr), thisValue)

Parameters:

Return value:

It returns the array element value if any of the elements in the array satisfy the condition, otherwise, it returns undefined.

JavaScript Array find() Method Example:

Here, the find() method finds a positive number.






// Input array contain some elements.
let array = [-10, -0.20, 0.30, -40, -50];
 
// Method (return element > 0).
let found = array.find(function (element) {
    return element > 0;
});
 
// Printing desired values.
console.log(found);

Output
0.3


Explanation:

The code searches for the first positive element in the `array`. The `find()` method iterates through the array, returning the first element greater than `0`. It logs the result to the console.

JavaScript Array find() Method Example:

Here, the find() method returns the value of the first element in the array that satisfies the provided testing method.




// Input array contain some elements.
let array = [10, 20, 30, 40, 50];
 
// Method (return element > 10).
let found = array.find(function (element) {
    return element > 20;
});
 
// Printing desired values.
console.log(found);

Output
30


Explanation:

The code searches for the first element in the `array` greater than `20`. It uses the `find()` method to iterate through the array and returns the first element that satisfies the condition. Finally, it logs the result (`30`) to the console.

JavaScript Array find() Method Example:

Here, whenever we need to get the value of the first element in the array that satisfies the provided testing method at that time we use find() method in JavaScript. 




// Input array contain some elements.
let array = [2, 7, 8, 9];
 
// Provided testing method (return element > 4).
let found = array.find(function (element) {
    return element > 4;
});
 
// Printing desired values.
console.log(found);

Output
7


Explanation:

The code aims to find the first element in the `array` greater than `4`. It employs the `find()` method, iterating through the array until a matching element is found. It logs the result (`7`) to the console.

JavaScript Array find() Method – UseCases:

Below are some UseCases of Array find() method.

1. JavaScript Array findIndex() Method

JavaScript Array.findIndex() method is used to return the first index of the element in a given array that satisfies the provided testing function (passed in by the user while calling). Otherwise, if no data is found then the value of -1 is returned.

2. Different ways to search an item in an array in JavaScript

These are the following methods:

3. Best Way to Find an Item in an Array in JavaScript

The most commonly used methods to find if an item is in a JavaScript array are:

Supported Browsers:

We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.


Article Tags :