Home >>JavaScript Array Reference >JavaScript Array findIndex() Method

JavaScript Array findIndex() Method

JavaScript Array findIndex() Method

The JavaScript array findIndex() method is used to return the index of the first element of the given array pass a test that satisfies the provided function condition. If no element satisfies the condition, it returns -1.

Syntax:
array.findIndex(function(currentValue, index, arr), thisValue)

Parameter Values

Parameter Description
function(currentValue, index, arr) It Required a function to be run for each element in the array.
Function arguments:
Argument Description
currentValue It Required the current value of the element
index This argument is optional and is used for the array index of the current element
arr It is optional and is used for the array object the current element belongs to
thisValue This parameter is optional and it is used for a value to be passed to the function to be used as its "this" value. The value "undefined" will be passed as its "this" value, If this parameter is empty

Browser Support

Method Chrome Edge Firefox Safari Opera
findIndex() 45.0 12.0 25.0 7.1 32.0
Here is an example of JavaScript array findIndex() Method:
<!DOCTYPE html>
<html>
<body>
<button onclick="myFindIn()">Click me</button>
<p id="findin"></p>
<script>
var ages = [8, 15, 18, 25];
function checkAdult(age) {
  return age >= 18;
}
function myFindIn() {
  document.getElementById("findin").innerHTML = ages.findIndex(checkAdult);
}
</script>
</body>
</html>
Output:

Example 2:
<!DOCTYPE html>
<html>
<body>
<script>
var finin1=[5,22,19,25,34];
var result=finin1.findIndex(x=>x>20);
document.writeln(result)
</script>
</body>
</html>
Output: