Array.prototype.find()
Array.prototype.find()
is a built-in method in JavaScript, which is used to search an array and return the first element that satisfies the provided testing function. This method returns undefined
if the element is not found in the array.
Syntax
array.find(function (currentValue) {
/**/
});
array.find(function (currentValue, index) {
/**/
});
array.find(function (currentValue, index, arr) {
/**/
});
array.find(function (currentValue, index, arr) {
/**/
}, thisValue);
array.find((currentValue) => {
/**/
});
array.find((currentValue, index) => {
/**/
});
array.find((currentValue, index, arr) => {
/**/
});
array.find((currentValue, index, arr) => {
/**/
}, thisValue);
Parameters
-
callback(currentValue, index, arr)
- A function that accepts up to three arguments. ThecurrentValue
represents the current element of the array,index
represents the index of the current element, andarr
represents the array thatfind()
was called upon. -
thisValue
(Optional) - An object that specifies the value to be used asthis
when executing the callback function. If not provided, the default value isundefined
.
return value
The first element of the array that satisfies the provided testing function; otherwise, undefined
is returned.
Examples
const numbers = [2, 5, 7, 8, 10];
const evenNumber = numbers.find((number) => number % 2 === 0);
console.log(evenNumber); // Output: 2
In the above example, find()
method is used to find the first even number in the array. The callback function checks whether the current number is divisible by 2 or not. If the current number is divisible by 2, then it is an even number, and find()
returns that number.
const users = [
{ id: 1, name: "John" },
{ id: 2, name: "Jane" },
{ id: 3, name: "Alex" },
];
const user = users.find((user) => user.id === 2);
console.log(user); // Output: { id: 2, name: 'Jane' }
In the above example, find()
method is used to find the object with id
equal to 2 in the array of objects. The callback function checks whether the id
property of the current object is equal to 2 or not. If the id
property is equal to 2, then find()
method returns that object.
Notes
-
find()
method is a built-in function of the Array object in JavaScript. -
It is used to find the first element in an array that satisfies a given condition.
-
find()
method takes a callback function as an argument, which is executed on each element of the array until the condition is satisfied. -
It does not modify the original array.
-
It works with arrays of any data type.
-
The
callback
function should returntrue
if the current element satisfies the condition, andfalse
otherwise. -
The
callback
function can take up to three arguments: the current element being processed, the index of the current element, and the array on which the find() method is called. -
It returns the value of the first element in the array that satisfies the condition, or
undefined
if no element satisfies the condition. -
If the array being searched is empty, the find() method will return
undefined
. -
find()
method stops searching the array as soon as it finds the first element that satisfies the condition. -
It was introduced in ECMAScript 2015 (ES6), so it may not be available in older browsers.
This page was updated on -
Found an error or have feedback on our docs?
Create an issue on GitHub and let us know! Your input helps improve our documentation for everyone in the community.
Report error, send feedback on Github