JavaScript | Array find() function

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)

Arguments
The 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 value
This 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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>

chevron_right


Output:

undefined

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);

chevron_right


Output:

> 7


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.