JavaScript Array findIndex() Method

Below is the example of the Array findIndex() method to find the index of positive number from an array.

  • 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];
      
        // function (return element > 0).
        var found = array.findIndex(function (element) {
            return element > 0;
        });
      
        // Printing desired values.
        document.write(found);
    </script>                    

    chevron_right

    
    

  • Output:
    2

The arr.findIndex() method used to return the index of the first element in a given array that satisfies the provided testing function. Otherwise, -1 is returned.

  • It does not execute the method once it finds an element satisfying the testing method.
  • It does not change the original array.

Syntax:

array.findIndex(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 the 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 index if any of the elements in the array pass the test, otherwise it returns -1.



Below examples illustrate the Array findIndex() function in JavaScript:

  • Example 1: In this example the method findIndex() finds all the indices that contain odd numbers. Since no odd numbers are present therefore it returns -1.
    function isOdd(element, index, array) {
      return (element%2 == 1);
    }
    
    print([4, 6, 8, 12].findIndex(isOdd)); 
    
    

    Output:

    -1
  • Example 2: In this example the method findIndex() finds all the indices that contain odd numbers. Since 7 is odd number therefore it returns its index .
    function isOdd(element, index, array) {
      return (element%2 == 1);
    }
    
    print([4, 6, 7, 12].findIndex(isOdd)); 
    
    

    Output:

    2

Codes for the above function are defined as follows:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
    // Input array contain elements
    var array = [10, 20, 30, 110, 60];
  
    // Testing method (element > 25).
    function finding_index(element) {
        return element > 25;
    }
  
    // Printing the index of element which is satisfies
    document.write(array.findIndex(finding_index));
</script>

chevron_right


Output:

2

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
    // Number is prime or not prime.
    function isPrime(n) {
        if (n === 1) {
            return false;
        } else if (n === 2) {
            return true;
        } else {
            for (var x = 2; x < n; x++) {
                if (n % x === 0) {
                    return false;
                }
            }
            return true;
        }
    }
  
    // Printing -1 because prime number is not found.
    document.write([4, 6, 8, 12].findIndex(isPrime));
    document.write("<br />")
    // Printing 2 the index of prime number (7) found.
    document.write([4, 6, 7, 12].findIndex(isPrime));
</script>

chevron_right


Output:

-1
2

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

  • Google Chrome 45
  • 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.