JavaScript | Array find() function

14 Mar, 2018
arr.find() function is used to find the first element from the array that satisfies the condition implemented by a function. If more than one element satisfies the condition then the first element satisfying the condition is returned. Suppose that you want to find the first odd number in the array. The argument function checks whether the argument passed to it is odd number or not . The find() function calls the argument function for every element of the array. The first odd number for which argument function returns true is reported by find() function as the answer. The syntax of the function is as follows:
Syntax:
arr.find(function(element, index, array), thisValue)

ArgumentsThe argument to this function is another function that defines the condition to be checked for each element of the array. This function itself takes three arguments:
array:This is the array on which the .filter() function was called.
index:This is the index of the current element being processed by the function.
element:This is the current element being processed by the function.
Another argument thisValue is used to tell the function to use this value when executing argument function.
Return valueThis function returns the first value from the array that satisfies the given condition. If no value satisfies the given condition, then it returns undefined as its answer.
Examples for the above function are provided below:
Example 1:
function isOdd(element, index, array) {
  return (element%2 == 1);
}

print([4, 6, 8, 12].find(isOdd));


Output:
undefined

In this example the function find() finds all the odd numbers in the array. Since no odd numbers are present, therefore it returns undefined .
Example 2:
function isOdd(element, index, array) {
  return (element%2 == 1);
}

print([4, 5, 8, 11].find(isOdd));

Output:
5

In this example the function find() finds the first occurrence of odd number in the array. Since first odd number is 5, therefore it returns it.
Codes for the above function are provided below:
Program 1:
// JavaScript to illustrate find() function  <script>function isOdd(element, index, array) {   return (element % 2 == 1); }  function func() {  var array = [ 4, 6, 8, 12 ];    // Checking for odd numbers and   // reporting the first odd number  document.write(array.find(isOdd));} func();</script>
Output:
undefined

Program 2:
<script>// JavaScript to illustrate find() function  function isOdd(element, index, array){    return (element % 2 == 1); }  function func() {  var array = [ 4, 5, 8, 11 ];    // Checking for odd numbers and   // reporting the first odd number  document.write(array.find(isOdd));} func();</script>
Output:
5

Application:Whenever we need to get the value of the first element in the array that satisfies the provided testing function that time we use Array.find() method in JavaScript.Let\’s see JavaScript program:
// input array contain some elements.var array = [2, 7, 8, 9];  // Here find function returns the value of // the first element in the array that satisfies // the provided testing function (return element > 4).var found = array.find(function(element) {  return element > 4;});  // Printing desired values.console.log(found);
Output:
> 7

javascript-array
JavaScript
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.