JavaScript 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.
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. It is an optional parameter.
- index: This is the index of the current element being processed by the function. It is an optional parameter.
- 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 the 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.
Example 1: In this example, the function find() finds all the odd numbers in the array. Since no odd numbers are present, therefore it returns undefined.
JavaScript
/* JavaScript to illustrate find() function */ function isOdd(element, index, array) { return (element % 2 == 1); } function func() { let array = [4, 6, 8, 12]; // Checking for odd numbers and // reporting the first odd number console.log(array.find(isOdd)); } func(); |
undefined
Example 2: In this example, the function find() finds the first occurrence of an odd number in the array. Since the first odd number is 5, therefore it returns it.
JavaScript
// JavaScript to illustrate find() function function isOdd(element, index, array) { return (element % 2 == 1); } function func() { let array = [4, 5, 8, 11]; // Checking for odd numbers and // reporting the first odd number console.log(array.find(isOdd)); } func(); |
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.
Example 3: In this example, the function find() finds the first occurrence of a number greater than 4 in the array. Since the number greater than 4 is 7, therefore it returns it.
JavaScript
// input array contain some elements. let 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). let found = array.find( function (element) { return element > 4; }); // Printing desired values. console.log(found); |
7
We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.
Supported Browsers: The browsers supported by JavaScript Array find() method are listed below:
- Google Chrome 45.0
- Microsoft Edge 12.0
- Mozilla Firefox 25.0
- Safari 7.1
- Opera 32.0
We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.