The Wayback Machine - https://web.archive.org/web/20220925030314/https://www.geeksforgeeks.org/javascript-array-find-method/amp/

JavaScript Array find() Method

The arr.find() method in Javascript is used to get the value of the first element in the array that satisfies the provided condition. It checks all the elements of the array and whichever first element satisfies the condition is going to print. This function will not work function having the empty array elements, also does not change the original array.

Syntax:

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

Parameters: This method accepts 5 parameters as mentioned above and described below:

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

We will understand the concept of the Javascript Array find() method through the examples.

Example 1: The below example illustrates the JavaScript Array find() method to find a positive number.




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

Output:

0.3

Example 2: Here, the arr.find() method in JavaScript returns the value of the first element in the array that satisfies the provided testing method.




<script>
    // Input array contain some elements.
    var array = [10, 20, 30, 40, 50];
 
    // Method (return element > 10).
    var found = array.find(function (element) {
        return element > 20;
    });
 
    // Printing desired values.
    document.write(found);
</script>

Output:

30

Example 3: In this example, 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 arr.find() method in JavaScript. 




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

Output:

7

Supported Browsers: The browsers supported by JavaScript Array find() method are listed below:

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 :