JavaScript Array find() Method
Last Updated :
09 Oct, 2024
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), thisValue)
Parameters:
- function(currentValue, index, arr): A function to execute on each value in the array until the first element satisfying the condition is found. It takes three parameters:
- currentValue: The current element being processed in the array.
- index (optional): The index of the current element being processed in the array.
- arr (optional): The array find() was called upon.
- thisValue (optional): A value to use as this when executing the callback function.
Return value:
- It returns the array element value if any of the elements in the array satisfy the condition, otherwise, it returns undefined.
If you’re looking to deepen your knowledge of array manipulation techniques, our JavaScript Course offers a comprehensive guide to array methods, giving you the skills to work with complex data structures.
Different Examples of find() Method
Example 1: In this example we searches for the first positive element in the array. The find() method iterates through the array, returning the first element greater than 0. It logs the result to the console.
JavaScript
// Input array contain some elements.
let array = [-10, -0.20, 0.30, -40, -50];
// Method (return element > 0).
let found = array.find(function (element) {
return element > 0;
});
// Printing desired values.
console.log(found);
Example 2: In this example we searches for the first element in the array greater than 20. It uses the find() method to iterate through the array and returns the first element that satisfies the condition. Finally, it logs the result (30) to the console.
JavaScript
// Input array contain some elements.
let array = [10, 20, 30, 40, 50];
// Method (return element > 10).
let found = array.find(function (element) {
return element > 20;
});
// Printing desired values.
console.log(found);
Example 3: In this example we aims to find the first element in the array greater than 4. It employs the find() method, iterating through the array until a matching element is found. It logs the result (`7`) to the console.
JavaScript
// Input array contain some elements.
let array = [2, 7, 8, 9];
// Provided testing method (return element > 4).
let found = array.find(function (element) {
return element > 4;
});
// Printing desired values.
console.log(found);
Supported Browsers:
JavaScript Array find() method- FAQs
How do I use the find() method in JavaScript?
find() is used like this: array.find(callback), where callback is a function that tests each element.
What does the find() method return if no match is found?
It returns undefined if no elements match the condition.
Can find() be used with arrays of objects?
Yes, you can use find() to locate an object that matches certain criteria.
How does find() differ from filter()?
find() returns the first matching element, while filter() returns an array of all matching elements.
Does find() modify the original array?
No, find() does not alter the original array; it only searches and retrieves an element.