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
Call
What 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().
// 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
Parameter
Description
callbackFn
Required. 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.
thisArg
Optional. The value to use as this when running callbackFn. It has no effect on an arrow function.
Return Value
Return Value
Description
undefined
Nothing 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.
Change on the first value. When the loop is at "a", the code removes "b" and adds "d".
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".
Same as the final Set. The loop ended with a, c, d, which is exactly what is left inside the Set.
Compatibility
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.
ES2015Standard since
Google Chrome38+ · Desktop & Mobile
Full support
Microsoft Edge12+ · Chromium based
Full support
Mozilla Firefox37+ · Desktop & Mobile
Full support
Apple Safari9+ · macOS & iOS
Full support
Opera25+ · Chromium based
Full support
Samsung InternetRecent versions · Android
Supported
Android WebView38+ · Chromium based
Supported
Node.js4+ · Server runtime
Supported
DenoRecent versions · JavaScript runtime
Supported
BunRecent versions · JavaScript runtime
Supported
Internet ExplorerIE 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.