Last Updated : 09 Jan, 2025
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:
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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4