The forEach() method runs a function on every array element for side effects—logging, summing into a variable, updating the DOM. It returns undefined, unlike map(). This tutorial covers syntax, five examples, when to choose forEach over other loops, and common pitfalls.
01
Syntax
forEach(fn)
02
Side effects
Not a mapper
03
Returns
undefined
04
No break
Use for loop
05
Index + array
Callback args
06
ES5
Wide support
Fundamentals
Introduction
Loops are everywhere in JavaScript. The forEach() method gives you a clean, readable way to visit each element without writing index variables or managing loop conditions manually.
Use it when you want to do something with each item (log it, push to another array, update UI)—not when you need a transformed array back. For that, reach for map() or filter().
Concept
Understanding the forEach() Method
array.forEach(callback) calls your callback once per element, passing (element, index, array). The callback runs in ascending index order from 0 to length - 1.
The method itself always returns undefined. Any result you need must be stored in an outer variable or performed as a side effect (like writing to the console or DOM).
💡
Beginner Tip
You cannot break out of forEach. If you find yourself wanting to stop early, switch to for...of or a classic for loop with break.
Each object is one element. The callback reads properties and performs actions like logging or DOM updates.
Applications
🚀 Common Use Cases
Console logging — inspect each element during development.
DOM updates — create or update nodes for each list item.
Accumulating totals — sum, count, or aggregate in outer variables.
API side effects — send analytics or fire requests per item.
Validation passes — check each entry and collect errors manually.
Event setup — attach listeners to each element in a NodeList converted to array.
🧠 How forEach() Runs
1
Start at index 0
Loop while index < array.length.
Init
2
Skip holes
Empty slots in sparse arrays are not visited.
Sparse
3
Invoke callback
Pass element, index, and the array reference.
Call
4
🔄
Return undefined
All side effects live inside the callback.
Important
📝 Notes
Return value is always undefined—not chainable like map.
break and continue do not work; use a for loop instead.
return in the callback only exits that one iteration.
Avoid adding/removing items while iterating—can cause skipped or duplicate visits.
Prefer reduce() for sums; filter() for conditional lists.
ES5—excellent browser support including IE9+.
Compatibility
Browser & Runtime Support
Array.prototype.forEach() has been available since ES5 (2009). It is one of the most widely supported array methods.
✓ Baseline · ES5
Array.prototype.forEach()
Supported in Chrome 1+, Firefox 1.5+, Safari 3+, Edge (all versions), IE 9+, and all modern Node.js versions.
99%Universal support
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
Array.forEach()Excellent
Bottom line: Safe to use everywhere except very old IE8 environments. No polyfill needed for modern projects.
Wrap Up
Conclusion
forEach() is the readable choice when you need to run code on every array element without building a new array. Remember it returns undefined and cannot be broken out of early.
When you need to check membership instead of iterating, the next method to learn is includes().
Use forEach for side effects (log, DOM, API calls)
Keep callbacks short and named when logic grows
Use map/filter/reduce when you need a result array
Use for...of when you need break or continue
Build new arrays in separate variables when needed
❌ Don’t
Assign forEach result expecting an array
Try to break out with break or return from outer scope
Mutate the array length during iteration carelessly
Replace map with forEach + push without reason
Use forEach for async sequential work (use for...of + await)
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Array.forEach()
Iterate arrays cleanly for side-effect-driven workflows.
5
Core concepts
📝01
Syntax
arr.forEach(fn)
API
🔄02
Side effects
Do something.
Purpose
∅03
undefined
No return.
Output
🚫04
No break
Use for loop.
Limit
🌟05
ES5
IE9+.
Support
❓ Frequently Asked Questions
forEach() runs a callback function once for every element in the array, in order. It is used for side effects like logging, updating DOM, or accumulating values in outer variables.
forEach() always returns undefined. It does not produce a new array. Use map(), filter(), or reduce() when you need a transformed result.
No. break and continue do not work inside forEach(). return only skips the rest of the current callback. Use for...of or a classic for loop if you need early exit.
map() returns a new array of transformed values. forEach() returns undefined and is meant for running code on each element without building a return value.
Yes. forEach() does not call the callback for holes (empty slots) in sparse arrays.
forEach() is ES5 (2009). It works in all modern browsers and has been supported in Internet Explorer 9+.
Did you know?
NodeList objects from document.querySelectorAll() also have a forEach method in modern browsers, so you can iterate DOM collections the same way as arrays—without converting to an array first.