JavaScript Array findIndex() Method
Last Updated :
05 Mar, 2024
JavaScript Array findIndex() method iterates over array elements, returning the index of the first element passing a test. If no match, returns -1. Doesn’t execute for empty elements, preserving the original array.
Array findIndex() Syntax
array.findIndex(function(currentValue, index, arr), thisValue);
Array findIndex() Parameters
- 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.
Array findIndex() 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:
- 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
let array = [-10, -0.20, 0.30, -40, -50];
let found = array.findIndex( function (element) {
return element > 0;
});
console.log(found);
|
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
let array = [10, 20, 30, 110, 60];
function finding_index(element) {
return element > 25;
}
console.log(array.findIndex(finding_index));
|
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
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 ;
}
}
console.log([4, 6, 8, 12].findIndex(isPrime));
console.log([4, 6, 7, 12].findIndex(isPrime));
|
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.
We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.
Supported Browsers:
Share your thoughts in the comments
Please Login to comment...