Set.prototype.forEach
Calls a function for each value of a Set.
Available since: ES2015
Syntax
set.forEach(callbackFn)Parameters
| Parameter | Type | Description |
|---|---|---|
| callbackFn | Function | A function that receives each value. |
Return value
undefinedReturns nothing.
Examples
Iterating over values:
const colors = new Set(['red', 'green']);
colors.forEach((color) => console.log(color));Output:
red
greenNotes
People usually write for (const value of set) instead — it lets you use break.
See also
- Map.prototype.forEach — Calls a function for each entry of a Map.
- for...of — Loops over the values of arrays, strings, Maps, Sets and other iterables.
In the tutorial
- Map and Set — The Map and Set collections in JavaScript: a dictionary with keys of any type and a set of unique values.