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

JavaScript Array find() Method

JavaScript Array find() Method

The JavaScript Array find() method is an inbuilt method in JavaScript which is used returns the value of the first element of the given array that satisfies the provided condition. It checks all the elements in the array and going to print the first element which satisfies the condition.

Syntax:

array.find(function(element))

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 It 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 It is Optional and is used for a value 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
find() 45.0 12.0 25.0 7.1 32.0

Here is an example of JavaScript array find() Method:

<!DOCTYPE html>
<html>
<body>
<button onclick="myFind()">Click me</button>
<p id="find"></p>
<script>
var ages = [3, 10, 20, 20];
function checkAdult(age) {
  return age >= 20;
}
function myFind() {
  document.getElementById("find").innerHTML = ages.find(checkAdult);
}
</script>
</body>
</html>
Output:

 

Example 2:

<!DOCTYPE html>
<html>
<body>
<script>
function Find1(value, index, arr) {
  var start = 2;
  while (start <= Math.sqrt(value)) {
    if (value % start++ < 1) {
      return false;
    }
  }
  return value > 1;
}
document.writeln([8, 4, 7, 22].find(Find1));
</script>
</body>
</html>
Output:

No Sidebar ads