Automatically gives you the current item, index, and the full array.
Cannot use break or continue β it always goes through the entire array.
More readable for simple array operations.
βοΈ Comparison Table
Feature
for Loop
forEach Loop
Syntax Complexity
Slightly more verbose
Cleaner and shorter
Access to index
Yes
Yes
Access to item
Yes (array[i])
Yes (passed directly)
Can break/continue
β Yes
β No
Async/Await support
β Yes
β Not directly supported
Works with non-arrays
β Yes
β No (only with arrays)
β Use for when:
You need to break early or skip iterations.
You need more manual control.
You want to await inside the loop (use for...of in that case).
β Use forEach when:
You just want to perform an action on every item.
You want clean and readable code.
You donβt need to exit early or use await.
π§ Example: Breaking loop
constarr=[1,2,3,4,5];// Using for loopfor (leti=0;i<arr.length;i++){if (arr[i]===3)break;console.log(arr[i]);// 1, 2}// Using forEach (will NOT break)arr.forEach((num)=>{if (num===3)return;// Only skips this iteration, NOT breaksconsole.log(num);// Will still print all except 3});
Top comments (0)
Subscribe
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Top comments (0)