The Array.find() method in JavaScript is a handy tool for searching through an array and retrieving the first element that satisfies a specified condition.
Syntax:
array.find(function(currentValue, index, arr),thisValue);
Parameters:
- function: It is the function of the array that works on each element.
- currentValue: This parameter holds the current element.
- index: It is an optional parameter that holds the index of the current element.
- arr: It is an optional parameter that holds the array object to which the current element belongs to.
- thisValue: This parameter is optional. If a value is to be passed to the function to be used as its “this” value the value “undefined” will be passed as its “this” value.
Return value:
It returns the array element value if any of the elements in the array satisfy the condition, otherwise, it returns undefined.
Example 1: In this example, the JavaScript Array find() method finds a positive number.
Javascript
let array = [-10, -0.20, 0.30, -40, -50];
let found = array.find(function (element) {
return element > 0;
});
console.log(found);
|
Example 2: In this example, the arr.find() method in JavaScript returns the value of the first element in the array that satisfies the provided testing method.
Javascript
let array = [10, 20, 30, 40, 50];
let found = array.find(function (element) {
return element > 20;
});
console.log(found);
|
Example 3: In this example, whenever we need to get the value of the first element in the array that satisfies the provided testing method at that time we use arr.find() method in JavaScript.
JavaScript
let array = [2, 7, 8, 9];
let found = array.find(function (element) {
return element > 4;
});
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:
- 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.
JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.
Learn to code easily with our course
Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
20 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...