JavaScript Set forEach() Method

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

Definition

set.forEach(callback) runs a function once for every value in the Set, in the order the values were added. It is the simplest way to visit each value when you do not need a result back.

The callback receives three arguments: the value, a key that is always the same as the value (this keeps the shape of Map.forEach()), and the set itself. The method returns undefined.

Remember
set.forEach((value, key, set) => { ... })

value → the current value
key   → the same as value (a Set has no keys)
set   → the Set being looped over

returns undefined
cannot be stopped early with break

Syntax

Call it on a Set and pass a function. You can also pass a second argument that becomes this inside a regular function.

JavaScript
const colors = new Set(["red", "green", "blue"]);

colors.forEach((value) => {
  console.log(value);
});
// red
// green
// blue
CallWhat it does
set.forEach(callback)Run callback(value, key, set) for every value.
set.forEach(callback, thisArg)Also set this inside a regular function callback.
for (const value of set) { ... }A loop that does the same job, and it allows break and continue.
Array.from(set).map(...)Turn the Set into an array first when you need map() or filter().

A standard JavaScript method (ES2015) on every Set, from Set.prototype. No import required. Siblings: add(), clear(), delete(), difference(), entries(), has(), intersection(), isDisjointFrom(), isSubsetOf(), isSupersetOf(), keys(), symmetricDifference(), union(), values(), [Symbol.iterator]().

Library declarations
JavaScript
// ECMAScript 2015 (ES6)
Set.prototype.forEach(callbackFn)
Set.prototype.forEach(callbackFn, thisArg)

// callbackFn: function (value, key, set)
// thisArg   : optional value to use as this inside callbackFn
// returns   : undefined

Parameters

ParameterDescription
callbackFnRequired. A function that is called for every value. It receives value, key (the same as value) and set. A TypeError is thrown if it is not a function.
thisArgOptional. The value to use as this when running callbackFn. It has no effect on an arrow function.

Return Value

Return ValueDescription
undefinedNothing is returned. Collect results yourself in a variable or an array inside the callback.

JavaScript Examples

Example 1 — Visit Every Value

Print the three callback arguments for each value, and check that the key is the same as the value.

JavaScript
const colors = new Set(["red", "green", "blue"]);

const result = colors.forEach((value, key, set) => {
  console.log(value + " | " + key + " | " + (set === colors));
});

console.log("Returned: " + result);
Try It Yourself

How It Works

  1. One call per value. The callback runs three times, once for each color, in the order they were added.
  2. Three arguments. value and key are the same text, and the third argument set is the same Set object as colors, so set === colors is true.
  3. No result. forEach() returns undefined, so the last line prints Returned: undefined.

Example 2 — Add Up the Values and Use thisArg

Because forEach() returns nothing, the code keeps a total in a variable. A second run uses thisArg with a regular function.

JavaScript
const scores = new Set([10, 20, 30]);

let total = 0;
scores.forEach((n) => {
  total += n;
});
console.log("Total: " + total);

const counter = { sum: 0 };
scores.forEach(function (n) {
  this.sum += n * 2;
}, counter);
console.log("Doubled sum: " + counter.sum);
Try It Yourself

How It Works

  1. Use an outer variable. The arrow function adds each number to total, which was created before the loop: 10 + 20 + 30 = 60.
  2. Pass thisArg. The second argument counter becomes this inside the regular function. That is why this.sum changes the counter object.
  3. Why not an arrow function? An arrow function ignores thisArg, so use a regular function when you need it.

Example 3 — Change the Set While Looping

A Set is visited live. A value added during the loop is visited too, and a value deleted before its turn is skipped.

JavaScript
const items = new Set(["a", "b", "c"]);
const visited = [];

items.forEach((value) => {
  visited.push(value);
  if (value === "a") {
    items.delete("b");
    items.add("d");
  }
});

console.log("Visited: " + visited.join(", "));
console.log("Final: " + Array.from(items).join(", "));
Try It Yourself

How It Works

  1. Change on the first value. When the loop is at "a", the code removes "b" and adds "d".
  2. Skip and extend. "b" was deleted before its turn, so it is never visited. "d" is added at the end, so the loop reaches it after "c".
  3. Same as the final Set. The loop ended with a, c, d, which is exactly what is left inside the Set.

Browser & Runtime Support

Set.prototype.forEach() is part of ES2015 (ES6), together with Set itself. Every current browser and Node.js has it.

✓ ES2015 · Widely supported

set.forEach()

Supported in Chrome 38+, Edge 12+, Firefox 37+, Safari 9+, Opera 25+ and Node.js 4+. Internet Explorer 11 only has a basic Set.

ES2015 Standard since
Google Chrome 38+ · Desktop & Mobile
Full support
Microsoft Edge 12+ · Chromium based
Full support
Mozilla Firefox 37+ · Desktop & Mobile
Full support
Apple Safari 9+ · macOS & iOS
Full support
Opera 25+ · Chromium based
Full support
Samsung Internet Recent versions · Android
Supported
Android WebView 38+ · Chromium based
Supported
Node.js 4+ · Server runtime
Supported
Deno Recent versions · JavaScript runtime
Supported
Bun Recent versions · JavaScript runtime
Supported
Internet Explorer IE 11 · basic Set only
Partial support
set.forEach() Excellent

Bottom line: Safe to use in every current browser and in Node.js. Only Internet Explorer needs special care.

Frequently Asked Questions

It passes the value, a key that is the same as the value, and the Set itself. A Set has no keys, so the second argument only exists to match Map.forEach(). Most code uses only the first argument.
It returns undefined. If you want a result, such as a total or a list, store it in a variable that you create before the loop.
No. break does not work inside the callback, and return only moves on to the next value. Use a for...of loop when you need to stop early.
Both visit the values in the order they were added. Use forEach() for a short action on every value. Use for...of when you need break, continue, return or await inside the loop.
Values you add during the loop are visited later in the same loop. Values you delete before the loop reaches them are skipped. A value is never visited twice in one run.

Next: has()

Learn how to check whether a value is in a Set, quickly and with a simple true or false answer.

has() 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.

14 people found this page helpful