Tutorialspoint.dev

JavaScript | Array.findIndex() Method

It returns index of the first element in a given array that satisfies the provided testing function. Otherwise -1 is returned.

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

Syntax:

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

Parameters:

  • function:- A function to be run for each element in the array.
  • currentValue:- The value of the current element.
  • index:- The array index of the current element.
  • array:- The array object with current element belongs to.
  • thisValue:- A value to be passed to the function to be used as its “this” value.
    If this parameter is empty, 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.
    JavaScript Version:
    ECMAScript 6
    Browser Support:
    Here in 2nd column int values is the version of the corresponding feature browser.



    Feature Basic support
    Chrome 45
    Edge Yes
    Firefox 25
    Internet Explorer No
    Opera 32
    Safari 8
    Android webview Yes
    Chrome for Android Yes
    Edge mobile Yes
    Firefox for Android 4
    Opera Android Yes
    iOS Safari 8

    Example 1:

    function isOdd(element, index, array) {
      return (element%2 == 1);
    }
    
    print([4, 6, 8, 12].findIndex(isOdd)); 
    
    

    Output:

    -1
    

    In this example the function findIndex() finds all the indices that contain odd numbers. Since no odd numbers are present therefore it returns -1.

    Example 2:

    function isOdd(element, index, array) {
      return (element%2 == 1);
    }
    
    print([4, 6, 7, 12].findIndex(isOdd)); 
    
    

    Output:

    2
    

    In this example the function findIndex() finds all the indices that contain odd numbers. Since 7 is odd number therefore it returns its index 2.

    Program:

    • Here the Array.findIndex() method in JavaScript returns the index of the first element in the array that satisfies the provided testing function. Otherwise -1 is returned.
      Let’s see JavaScript program:

      // input array contain some elements whose index
      // need to find which satisfy the provided 
      // testing function (element > 25).
      var array = [ 10, 20, 30, 110, 60 ];
        
      // calling function for testing.
      function finding_index(element) 
      { return element > 25; }
        
      // Printing the index of element which is satisfies
      // the provided testing function (element > 25)
      console.log(array.findIndex(finding_index));

      Output:

      > 2
      
    • Application:

    • This Array.findIndex() Method can be used to find the index of an element in the array that is a prime number (or returns -1 if there is no prime number).
      Let’s see JavaScript program:

      // function isPrime is used to find which
      // 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.
      console.log([ 4, 6, 8, 12 ].findIndex(isPrime));
        
      // Printing 2 the index of prime number (7) found.
      console.log([ 4, 6, 7, 12 ].findIndex(isPrime));

      Output:

      > -1
      > 2
      


This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter