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:
- 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)));
|
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)));
|
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
let array = [-10, -0.20, 0.30, -40, -50];
let found = array.findIndex(function (element) {
return element > 0;
});
console.log(found);
|
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
let array = [10, 20, 30, 110, 60];
function finding_index(element) {
return element > 25;
}
console.log(array.findIndex(finding_index));
|
Example 5: In this example, 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));
|
Example 6: 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]);
|
We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.
Supported Browsers:
- 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.
Last Updated :
20 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...