JavaScript Array find()
Example
Get the value of the first element in the array that has a value over 18:
const ages = [3, 10, 18, 20];
function checkAge(age) {
return age > 18;
}
function myFunction() {
document.getElementById("demo").innerHTML = ages.find(checkAge);
}
Try it Yourself »
More "Try it Yourself" examples below.
Definition and Usage
The find() method returns the value of the array element that passes a
test (provided by a function).
The method executes the function once for each element present in the array:
- If it finds an array element where the function returns a true value, find() returns the value of that array element (and does not check the remaining values)
- Otherwise it returns undefined
find() does not execute the function for empty array elements.
find() does not change the original array.
Browser Support
The numbers in the table specify the first browser version that fully supports the method:
| Chrome 45 | Edge 12 | Firefox 25 | Safari 7.1 | Opera 32 |
| Sep 2015 | Jul 2015 | Jul 2014 | Sep 2014 | Sep 2015 |
The find() method is not supported in Internet Explorer.
Syntax
array.find(function(currentValue, index, arr),thisValue)
Parameter Values
| Parameter | Description | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| function(currentValue, index, arr) | Required. A function to be run for each element in the array. Function arguments:
|
||||||||
| thisValue | Optional. A value to be passed to the function to be used as its "this" value. If this parameter is empty, the value "undefined" will be passed as its "this" value |
Technical Details
| Return Value: | Returns the array element value if any of the elements in the array pass the test, otherwise it returns undefined |
|---|---|
| JavaScript Version: | ECMAScript 6 |
More Examples
Example
Get the value of the first element in the array that has a value above a specific number:
<p><input type="number" id="ageToCheck" value="18"></p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
const ages = [4, 12, 16, 20];
function checkAge(age) {
return age > document.getElementById("ageToCheck").value;
}
function myFunction() {
document.getElementById("demo").innerHTML = ages.find(checkAge);
}
</script>
Try it Yourself »

