JavaScript Array find() Method
JavaScript find()
method is used to retrieve the first element in an array that satisfies a specified condition.
Array find() method executes a provided function once for each element in the array until it finds one where the function returns a truthy value. The find()
method does not change the original array.
Array find() Method 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.
JavaScript Array find() Method Example:
Here, the 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);
|
Explanation:
The code 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 Array find() Method Example:
Here, the find() method 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);
|
Explanation:
The code 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 Array find() Method Example:
Here, 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 find() method in JavaScript.
JavaScript
let array = [2, 7, 8, 9];
let found = array.find( function (element) {
return element > 4;
});
console.log(found);
|
Explanation:
The code 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 Array find() Method – UseCases:
Below are some UseCases of Array find() method.
JavaScript Array.findIndex() method is used to return the first index of the element in a given array that satisfies the provided testing function (passed in by the user while calling). Otherwise, if no data is found then the value of -1 is returned.
These are the following methods:
The most commonly used methods to find if an item is in a JavaScript array are:
Supported Browsers:
We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.
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.
Last Updated :
27 Feb, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...