JavaScript Array findIndex() method
The JavaScript array findIndex() method returns the index of first element of the given array that satisfies the provided function condition. It returns -1, if no element satisfies the condition.
Syntax
The findIndex() method is represented by the following syntax:
Parameter
callback – It represents the function that executes each element.
value – The current element of an array.
index – It is optional. The index of current element.
arr – It is optional. The array on which findIndex() method operated.
thisArg – It is optional. The value to use as this while executing callback.
Return
The index of first element of the array that satisfies the function condition.
JavaScript Array findIndex() method example
Let’s see some examples of findIndex() method.
Example 1
Let’s see a simple example of findIndex() method.
Output:
1
Example 2
In this example, we will find the index of prime number using findIndex() method.
Output:
2