Javascript Array Find method is used to get the value of the first element in the array that satisfies the given condition.
It a built- method and returns the value of the first element in an array that passes a given test condition as a function.
Note:
- find() method doesn’t t execute the function for empty arrays.
- And doesn’t change the original array.
Syntax
array.find(function(element))
Parameter Values
- element:- Which you want find and its current element in the array.
Return value
It returns the value of the first element in the provided array that satisfies the provided testing function
Examples of JavaScript array find method
let’s see example of Array find() function in JS.
The find() method executes the function once for each element present in the array: If it finds an array element then it will return the value of that element and doesn’t t check the remaining values. Otherwise, it returns the undefined output.
<!DOCTYPE html>
<html>
<head>
<title> Example</title>
<script type="text/javascript">
// input array contain some elements.
var array = [10, 20, 30, 40, 50];
// function (return element > 10).
var found = array.find(function(element) {
return element > 20;
});
// show output in alert
alert(found)
</script>
</head>
</html>
Output:
Do comment if you have any doubts and suggestion on this tutorial.
Note: The All JS Examples codes are tested on the Safari browser (Version 12.0.2) and Chrome.
OS: macOS 10.14 Mojave
Code: HTML 5 Version