Difference between for..of loop and forEach method in JavaScript

The forEach
method and the for...of
loop are both used to iterate over collections in JavaScript, but they have some key differences. Here’s a comparison of the two:
1. Syntax and Usage
forEach
Method:It is a method available on arrays.
It takes a callback function as an argument and executes that function for each element in the array.
const array = [1, 2, 3];
array.forEach((element) => {
console.log(element); // Outputs: 1, 2, 3
});
for...of
Loop:It is a loop construct that can be used with any iterable (arrays, strings, maps, sets, etc.).
It directly iterates over the values of the iterable.
const array = [1, 2, 3];
for (const element of array) {
console.log(element); // Outputs: 1, 2, 3
}
2. Return Value
forEach
:- Does not return a value. It always returns
undefined
.
- Does not return a value. It always returns
for...of
:- Does not return a value either, but it allows you to use
break
andcontinue
statements to control the flow of the loop.
- Does not return a value either, but it allows you to use
3. Break and Continue
forEach
:- You cannot use
break
,continue
, orreturn
to exit the loop early. If you need to stop the iteration, you must use a workaround (like throwing an exception).
- You cannot use
for...of
:- You can use
break
to exit the loop andcontinue
to skip to the next iteration.
- You can use
const array = [1, 2, 3, 4, 5];
for (const element of array) {
if (element === 3) break; // Exits the loop when element is 3
console.log(element); // Outputs: 1, 2
}
function findLongString(strings, minLength) {
for (const str of strings) {
if (str.length > minLength) {
return str; // Return the first string longer than minLength
}
}
return null; // Return null if no such string is found
}
const stringArray = ["hi", "hello", "world", "JavaScript"];
const longString = findLongString(stringArray, 5);
console.log(longString); // Outputs: JavaScript
const words = ["hi", "bye", "hello", "world", "skip", "this", "example"];
for (const word of words) {
if (word.length < 4) {
continue; // Skip words with less than 4 characters
}
console.log(word); // Outputs: hello, world, skip, this, example
}
4. Performance
forEach
:- Generally, it may have a slight overhead due to the function call for each element, especially for large arrays.
for...of
:- Typically faster for large collections since it does not involve function calls.
5. Scope of this
forEach
:- The value of
this
inside the callback function is determined by how the function is called. You can usebind
to setthis
explicitly.
- The value of
for...of
:- The value of
this
is determined by the surrounding context, and it behaves like a regular loop.
- The value of
Conclusion
Use
forEach
when you want to perform an operation on each element of an array without needing to break out of the loop.Use
for...of
when you need more control over the iteration, such as breaking out of the loop or iterating over different types of iterables.
Both methods are useful, and the choice between them depends on the specific requirements of your code.
Subscribe to my newsletter
Read articles from pushpesh kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
