JavaScript Array findIndex() Method

The findIndex() method in JavaScript is a powerful tool for locating the first element in an array that satisfies a provided testing function. This guide provides a comprehensive overview of how to use findIndex() effectively, enhancing your ability to manipulate arrays.

Syntax

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

Parameters

ParameterDescription
functionThe function of the array that operates on each element.
currentValueHolds the current element during iteration.
index(Optional) Index of the current element.
arr(Optional) The array object to which the current element belongs.
thisValue(Optional) Value to be passed as the “this” value to the function. If not provided, defaults to “undefined”.

Return value

It returns the array element index if any of the elements in the array pass the test, otherwise, it returns -1.

Examples

Example 1: Finding Indices that contain odd numbers

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

Explanation:

  • In the above example we defines a function isOdd to check if an element is odd.
  • Uses the findIndex method on an array to find the index of the first odd element.
  • Returns -1 since there are no odd elements in the array.

Example 2: Finding Indices that contain positive numbers

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

Explanation

  • In the above example we define an array containing positive and negative numbers.
  • Uses the findIndex method to locate the index of the first positive number.
  • Identifies 0.30 at index 2 as the first positive number and prints its index.

Example 3: Finding Indices that contain numbers greater than 25

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

Explanation

  • In the above example we defines an array of numbers and a function to test if an element is greater than 25.
  • Utilizes the findIndex method to locate the index of the first element greater than 25.
  • Prints the index of the first element that satisfies the condition.

Example 4: Finding Indices that contain prime numbers.

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

Explanation:

  • In the above example we defines a function isPrime to check if a number is prime by iterating up to n and returning true if divisible only by 1 and itself.
  • Uses findIndex to find the index of the first prime number in two arrays, printing -1 and 2 respectively.
  • -1 indicates no prime found in the first array, and 2 is the index of the first prime in the second array.

Common Use Cases

While specific examples will be detailed in the approaches section, here’s a brief overview:

  • Finding an Object: Locate the index of an object in an array based on a property value.
  • Condition-Based Search: Identify the index of the first element meeting a condition.

Mastering the findIndex() method enhances your ability to work with arrays efficiently. This guide introduces the importance and prerequisites, setting the stage for detailed method explanations. By focusing on practical applications, you can improve your JavaScript skills and code effectiveness.

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

Supported Browsers:



Contact Us