Saturday, November 16, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Array findIndex() Method

JavaScript Array findIndex() Method

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

  • 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 to which the current element belongs.
  • thisValue: This parameter is optional if a value is 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.

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.

Javascript




function isOdd(element, index, array) {
    return (element % 2 == 1);
}
 
console.log(([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 an odd number therefore it returns its index.

Javascript




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


Output:

2

Example 3: In this example, the method findIndex() finds all the indices that contain positive numbers. Since 0.30 is a positive number therefore it returns its index.

JavaScript




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


Output:

2

Example 4: In this example, the method findIndex() finds all the indices that contain numbers greater than 25. Since 30 is greater than 25 therefore it returns its index.

Javascript




// Input array contain elements
let 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
console.log(array.findIndex(finding_index));


Output:

2

Example: In this example, the method findIndex() finds all the indices that contain prime numbers.

JavaScript




// Number is prime or not prime.
function isPrime(n) {
    if (n === 1) {
        return false;
    } else if (n === 2) {
        return true;
    } else {
        for (let 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

Example: In this example, we will use arrow functions to find out the index of an element from an array.

Javascript




let fruits_array = ["apple", "banana", "mango",
                    "banana", "grapes"];
let searching_index = fruits_array.findIndex
    (fruit => fruit === "mango");
console.log(searching_index);
console.log(fruits_array[searching_index]);


Output:

2
mango

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

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

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

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.

RELATED ARTICLES

Most Popular

Recent Comments