JavaScript Array findIndex() Method (original) (raw)

Last Updated : 09 Jul, 2024

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

Parameter Description
function The function of the array that operates on each element.
currentValue Holds 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)));

`

Explanation:

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

`

Explanation

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

`

Explanation

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

`

Explanation:

Common Use Cases

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

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: