JavaScript | Array findIndex() function

arr.findIndex() function is used to find the index of the first element that satisfies the condition checked by the argument function from the given array. Suppose you want to find the index of the first element of the array that is even. Then the function as an argument to findIndex() checks for even numbers for all the elements of the array.The first element that this function returns as even, its index will be returned by the findIndex() function as the answer.The syntax of the function is as follows:

array.findIndex(function(element, index, arr), thisValue)

Arguments
The argument to this function is another function that defines the condition to be checked for each element of the array. This function itself takes three arguments:

  • array
  • This is the array on which the .findindex() function was called.

  • index
  • This is the index of the current element being processed by the function.

  • element
  • This is the current element being processed by the function.

Another argument thisValue is used to tell the function to use this value when executing argument function.

Return value
This function returns the index value of the first element that satisfies the given condition. If no element satisfies the condition then it returns -1

Examples for the above function are provided below:

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.

Codes for the above function are provided below:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// JavaScript to illustrate findIndex() function
<script>
function isOdd(element, index, array) {
  return (element % 2 == 1);
}
  
function func() {
    var array = [4, 6, 8, 12];
    document.write(array.findIndex(isOdd));
}
func();
</script>

chevron_right


Output:

-1

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// JavaScript to illustrate findIndex() function
<script>
function isOdd(element, index, array) 
{
  return (element % 2 == 1);
}
  
function func() {
    var array = [4, 6, 7, 12];
    document.write(array.findIndex(isOdd));
}
func();
</script>

chevron_right


Output:

2


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.