Home >>JavaScript Array Reference >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 | Description | ||||||||
---|---|---|---|---|---|---|---|---|---|
function(currentValue, index, arr) | It Required a function to be run for each element in the array. Function arguments:
|
||||||||
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 |
Method | Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
findIndex() | 45.0 | 12.0 | 25.0 | 7.1 | 32.0 |
<!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>
<!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>