iterator.forEach(callback) walks every value from the iterator and runs your function once for each one. It is a simple way to "do something" with each value, such as print it or push it into an array.
The method always returns undefined. It also consumes the iterator: after forEach() finishes, that iterator has no more values left to read.
Remember
iterator.forEach(callback)
runs callback once per value
return value of callback → ignored
forEach itself returns → undefined
Consumes the whole iterator.
Callback gets (value, index).
Syntax
Call it on an iterator and pass a function to run for each value.
The Try It page first checks that iterator.forEach() exists, because Iterator helpers are still new. In an older browser it prints a short explanation and the output above. In a current browser it runs the code for real.
How It Works
Make an iterator.[1, 2, 3].values() yields 1, then 2, then 3.
Call forEach(). The callback runs once per value and prints it.
No return value.forEach() itself returns undefined; the useful work is inside the callback.
Example 2 — Collect Values Into an Array
Push each value into an array so you can see what forEach() visited.
The Try It page first checks that iterator.forEach() exists, because Iterator helpers are still new. In an older browser it prints a short explanation and the output above. In a current browser it runs the code for real.
How It Works
Prepare a box.seen starts as an empty array.
Push inside the callback. Each value from the iterator is added to seen.
Check the result. All five numbers were visited, so the array length is 5.
The Try It page first checks that iterator.forEach() exists, because Iterator helpers are still new. In an older browser it prints a short explanation and the output above. In a current browser it runs the code for real.
How It Works
Two arguments. The callback receives n (value) and i (index from 0).
Walk in order. The first call is index 0 with 10, then 1 with 20, then 2 with 30.
Iterator finished. After the last value, forEach() ends and returns undefined.
Compatibility
Browser & Runtime Support
Iterator.prototype.forEach() is an Iterator helper (Baseline Newly available since March 2025). It works in current browsers and in Node.js 22 or newer.
✓ Baseline 2025 · Newly available
iterator.forEach()
Supported in Chrome 122+, Edge 122+, Firefox 131+, Safari 18.4+, Opera 108+ and Node.js 22+. Internet Explorer never got it.
2025Standard since
Google Chrome122+ · Desktop & Mobile
Full support
Microsoft Edge122+ · Chromium based
Full support
Mozilla Firefox131+ · Desktop & Mobile
Full support
Apple Safari18.4+ · macOS & iOS
Full support
Opera108+ · Chromium based
Full support
Samsung Internet26+ · Android
Supported
Android WebView122+ · Chromium based
Supported
Node.js22+ · Server runtime
Supported
DenoRecent versions · JavaScript runtime
Supported
BunRecent versions · JavaScript runtime
Supported
Internet ExplorerNo native support
Not supported
iterator.forEach()Current browsers
Bottom line: Check support with a typeof test before you use it in older browsers. Prefer chaining helpers and calling toArray() last.
Frequently Asked Questions
It runs your callback once for every value from the iterator. It always returns undefined and consumes the whole iterator.
No. Unlike map() or filter(), forEach() returns undefined. Use it for side effects, not for chaining more helpers on a result.
No. forEach() always visits every remaining value. If you need to stop early, use a for...of loop with break, or a helper like find() or some().
No. It keeps going until the iterator ends, so an infinite source never finishes. Prefer lazy helpers or a loop you can stop yourself.
The idea is the same, but it runs on an iterator. Check support with typeof Iterator.prototype.forEach before using it in older browsers.