JavaScript Array find() function
Last Updated :
07 Jul, 2023
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();
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();
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);
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.
Similar Reads
JavaScript Array findIndex() Function JavaScript arr.findIndex() function is used to find the index of the first element that satisfies the condition checked by the argument function from the given array. Suppose you want to find the index of the first element of the array that is even. Syntax: array.findIndex(function(element, index, a
3 min read
JavaScript - Find an Item in an Array Here are the various methods to find an item in an array in JavaScript1. Using the includes() methodThe includes() method checks if an array includes a certain value, returning the boolean value true or false accordingly.JavaScriptconst a = ['apple', 'banana', 'orange']; console.log(a.includes('bana
4 min read
JavaScript Array findIndex() Method The findIndex() method in JavaScript is a powerful tool for locating the first element in an array that satisfies a provided testing function. This guide provides a comprehensive overview of how to use findIndex() effectively, enhancing your ability to manipulate arrays. Syntax array.findIndex(funct
5 min read
JavaScript Array find() Method The find() method in JavaScript looks through an array and returns the first item that meets a specific condition you provide. If no item matches, it returns undefined. It skips any empty space in the array and doesnât alter the original array.Syntax:array.find(function(currentValue, index, arr), th
3 min read
JavaScript Array findLastIndex() Method The Javascript findLastIndex() method is used to find the index of the last element in an array that matches the specified condition. It works by iterating over the array from the end to the beginning and returning the index of the first element that satisfies the condition specified in a callback f
2 min read