Last updated: Mar 4, 2024
Reading timeยท3 min
for...of
for
To find the first array element that matches a condition:
Array.find()
method to iterate over the array.find()
method returns the first array element that meets the condition.const arr = [1, 2, 3, 4, 5]; const result = arr.find(element => { return element > 2; }); console.log(result); // ๐๏ธ 3
The function we passed to the Array.find() method gets called with each element in the array until it returns a truthy value or iterates over the entire array.
The falsy values in JavaScript are: false
, null
, undefined
, 0
, ""
(empty string), NaN
(not a number).
All other values are truthy.
If the function we passed to the Array.find()
method returns a truthy value at
least once, the find()
method short-circuits and returns the corresponding
array element.
In the example, we check if the current array element is greater than 2
.
The first element in the array that is greater than 2
is 3
.
const arr = [1, 2, 3, 4, 5]; const result = arr.find(element => { return element > 2; }); console.log(result); // ๐๏ธ 3
As soon as the condition is met, the find()
method short-circuits and returns
the value.
If the function never returns a truthy value, the find()
method will have to
iterate over the entire array and will return undefined
.
const arr = [1, 2, 3, 4, 5]; const result = arr.find(element => { return element > 100; }); console.log(result); // ๐๏ธ undefined
The implemented condition is never satisfied, so the find()
method iterated
over the entire array and ended up returning undefined
.
You also have access to the index of the current iteration when using
Array.find()
.
const arr = [1, 2, 3, 4, 5]; const result = arr.find((element, index) => { console.log(index); // ๐๏ธ 0, 1, 2, 3, 4 return element > 100; }); console.log(result); // ๐๏ธ undefined
The callback function gets called with the element, the current index and the array that's being iterated.
for...of
This is a three-step process:
for...of
loop to iterate over the array.const arr = [1, 2, 3, 4, 5]; let result; for (const element of arr) { if (element > 2) { result = element; break; } } console.log(result); // ๐๏ธ 3
Notice that we declared the result
variable using let
.
Variables declared using const
cannot be reassigned.
The for...of statement is
used to loop over iterable objects like arrays, strings, Map
, Set
and
NodeList
objects and generators
.
On each iteration, we check if the current element meets a condition.
If the condition is met, we assign the element to a variable and exit the
for...of
loop.
The break statement enables us to break out of the loop to avoid iterating needlessly after we've found the matching element.
You can also use a basic for
loop to find the first array element that meets a
condition.
for
This is a three-step process:
for
loop to iterate over the array.const arr = [1, 2, 3, 4, 5]; let result; for (let index = 0; index < arr.length; index++) { if (arr[index] > 2) { result = arr[index]; break; } } console.log(result); // ๐๏ธ 3
Instead of using a for...of
loop this solution uses a basic for
loop to find
the first matching element.
If no element meets the condition, the result
variable remains set to
undefined
.
Which approach you pick is a matter of personal preference. I'd use the
Array.find()
method because I find it more direct and intuitive.
You can learn more about the related topics by checking out the following tutorials: