JavaScript | Array.findIndex() Method
It returns index of the first element in a given array that satisfies the provided testing function. Otherwise -1 is returned.
- It does not execute the function once it finds an element satisfying the testing function.
- It does not change the original array.
Syntax:
array.findIndex(function(currentValue, index, arr), thisValue)
Parameters:
- function:- A function to be run for each element in the array.
- currentValue:- The value of the current element.
- index:- The array index of the current element.
- array:- The array object with current element belongs to.
- thisValue:- A value to be passed to the function to be used as its “this” value.
If this parameter is empty, 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.
JavaScript Version:
ECMAScript 6
Browser Support:
Here in 2nd column int values is the version of the corresponding feature browser.
| Feature | Basic support |
|---|---|
| Chrome | 45 |
| Edge | Yes |
| Firefox | 25 |
| Internet Explorer | No |
| Opera | 32 |
| Safari | 8 |
| Android webview | Yes |
| Chrome for Android | Yes |
| Edge mobile | Yes |
| Firefox for Android | 4 |
| Opera Android | Yes |
| iOS Safari | 8 |
Example 1:
function isOdd(element, index, array) {
return (element%2 == 1);
}
print([4, 6, 8, 12].findIndex(isOdd));
Output:
-1
In this example the function findIndex() finds all the indices that contain odd numbers. Since no odd numbers are present therefore it returns -1.
Example 2:
function isOdd(element, index, array) {
return (element%2 == 1);
}
print([4, 6, 7, 12].findIndex(isOdd));
Output:
2
In this example the function findIndex() finds all the indices that contain odd numbers. Since 7 is odd number therefore it returns its index 2.
Program:
- Here the Array.findIndex() method in JavaScript returns the index of the first element in the array that satisfies the provided testing function. Otherwise -1 is returned.
Let’s see JavaScript program:// input array contain some elements whose index// need to find which satisfy the provided// testing function (element > 25).vararray = [ 10, 20, 30, 110, 60 ];// calling function for testing.functionfinding_index(element){returnelement > 25; }// Printing the index of element which is satisfies// the provided testing function (element > 25)console.log(array.findIndex(finding_index));chevron_rightfilter_noneOutput:
> 2
- This Array.findIndex() Method can be used to find the index of an element in the array that is a prime number (or returns -1 if there is no prime number).
Let’s see JavaScript program:// function isPrime is used to find which// number is prime or not prime.functionisPrime(n) {if(n === 1) {returnfalse;}elseif(n === 2) {returntrue;}else{for(varx = 2; x < n; x++) {if(n % x === 0) {returnfalse;}}returntrue;}}// 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));chevron_rightfilter_noneOutput:
> -1 > 2
Recommended Posts:
- JavaScript | Array.from() method
- JavaScript | Replace() Method
- JavaScript | Array map() Method
- JavaScript | getTime() Method
- JavaScript | Array from() Method
- JavaScript | compile() Method
- JavaScript | Sort() method
- JavaScript | exec() Method
- JavaScript | Array.find() Method
- JavaScript | Array.splice() Method
- JavaScript | Array.fill() Method
- JavaScript | String includes() Method
- JavaScript | date.getDay() method
- JavaScript | Array reduce() Method
- Javascript | MouseEvent getModifierState() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



