JavaScript Iterator forEach() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Instance method

Definition

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.

JavaScript
const nums = [1, 2, 3].values();

nums.forEach((n) => {
  console.log(n);
});
CallWhat it does
iterator.forEach(callback)Run callback once for every value. Returns undefined.
[1, 2, 3].values().forEach(fn)Get an iterator from an array, then visit each value.
callback(value, index)Your function receives the current value and its zero-based index.

A standard JavaScript method (Iterator helpers, Baseline 2025) on every iterator, from Iterator.prototype. No import required. Siblings: Iterator.concat(), Iterator.from(), Iterator.zip(), Iterator.zipKeyed(), chunks(), drop(), every(), filter(), find(), flatMap(), includes(), join(), map(), reduce(), some(), take(), toArray(), windows(), [Symbol.dispose](), [Symbol.iterator]().

Library declarations
JavaScript
// Iterator helpers (Baseline 2025)
Iterator.prototype.forEach(callbackFn)

// callbackFn : (value, index) => void
// returns    : undefined

Parameters

ParameterDescription
callbackFnRequired. A function called once for each value. Its return value is ignored. It receives value (the current item) and index (starting at 0).

Return Value

Return ValueDescription
undefinedAlways. forEach() is for side effects (logging, collecting into another structure), not for building a new iterator.

JavaScript Examples

Example 1 — Run a Callback for Each Value

Print every number from an iterator with forEach().

JavaScript
const nums = [1, 2, 3].values();

nums.forEach((n) => {
  console.log("Value: " + n);
});
Try It Yourself

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

  1. Make an iterator. [1, 2, 3].values() yields 1, then 2, then 3.
  2. Call forEach(). The callback runs once per value and prints it.
  3. 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.

JavaScript
const nums = [1, 2, 3, 4, 5].values();
const seen = [];

nums.forEach((n) => {
  seen.push(n);
});

console.log("Seen: " + seen.join(", "));
console.log("Count: " + seen.length);
Try It Yourself

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

  1. Prepare a box. seen starts as an empty array.
  2. Push inside the callback. Each value from the iterator is added to seen.
  3. Check the result. All five numbers were visited, so the array length is 5.

Example 3 — Use Value and Index Together

Print both the index and the value for each step.

JavaScript
const nums = [10, 20, 30].values();

nums.forEach((n, i) => {
  console.log("Index " + i + " => " + n);
});
Try It Yourself

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

  1. Two arguments. The callback receives n (value) and i (index from 0).
  2. Walk in order. The first call is index 0 with 10, then 1 with 20, then 2 with 30.
  3. Iterator finished. After the last value, forEach() ends and returns undefined.

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.

2025 Standard since
Google Chrome 122+ · Desktop & Mobile
Full support
Microsoft Edge 122+ · Chromium based
Full support
Mozilla Firefox 131+ · Desktop & Mobile
Full support
Apple Safari 18.4+ · macOS & iOS
Full support
Opera 108+ · Chromium based
Full support
Samsung Internet 26+ · Android
Supported
Android WebView 122+ · Chromium based
Supported
Node.js 22+ · Server runtime
Supported
Deno Recent versions · JavaScript runtime
Supported
Bun Recent versions · JavaScript runtime
Supported
Internet Explorer No 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.

Next: includes()

Learn how to ask whether an iterator contains a value, including how NaN is handled.

includes() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful