The find()
method returns the value of the first array element that passes a test function.
This example finds (returns the value of) the first element that is larger than 18:
Example
const numbers = [4, 9, 16, 25, 29];
let first = numbers.find(myFunction);
function myFunction(value, index, array) {
return value > 18;
}
let first = numbers.find(myFunction);
function myFunction(value, index, array) {
return value > 18;
}
Note that the function takes 3 arguments:
- The item value
- The item index
- The array itself
Array.find()
is not supported in older browsers. The first browser versions with full support is listed below.
Practice Excercise Practice now