JavaScript Array find() Method

Below is the example of the Array find() method to find a positive number.

  • Example:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <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>

    chevron_right

    
    

  • Output:
    0.3

The arr.find() method 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 the first element satisfies the condition is going to print.

Syntax:

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

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

  • function: It is the function of the array that works on each element.
  • currentValue: This parameter holds the current element.
  • index: It is an optional parameter that holds the index of current element.
  • arr: It is an optional parameter that holds the array object the current element belongs to.
  • thisValue: This parameter is optional, if a value to be passed to the function to be used as its “this” value else the value “undefined” will be passed as its “this” value.

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



Below examples illustrate the JavaScript Array find() method in JavaScript:

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>

chevron_right


Output:

30

Example 2: 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>

chevron_right


Output:

7

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

  • Google Chrome 45.0
  • Microsoft Edge 12.0
  • Mozilla Firefox 25.0
  • Safari 7.1
  • Opera 32.0

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.