The JavaScript array find() method returns the first element of the given array that satisfies the provided function condition.
The JavaScript Array find() method returns the value of the first array element that satisfies the provided test function.
The find() method executes the function once for each element present in the array:
Parameters: This method accepts five parameters as mentioned above and described below:
array.find(function(currentValue, index, arr),thisValue)
Get the value of the first element in the array that has a value above a specific number:
<p>Minimum age: <input type="number" id="ageToCheck" value="18"></p>
<button onclick="myFunction()">Try it</button>
<p>Any ages above: <span id="demo"></span></p>
<script>
var ages = [4, 12, 16, 20];
function checkAdult(age) {
return age >= document.getElementById("ageToCheck").value;
}
function myFunction() {
document.getElementById("demo").innerHTML = ages.find(checkAdult);
}
</script>