JavaScript Reference ยท Collections
Set iteration
How to loop over a JavaScript Set with for...of, forEach(), values(), keys() and entries(), including insertion order and what happens when you change a Set while looping.
A Set remembers the order in which its values were first added, and every way of looping over it follows that order. You can use for...of, the spread operator, forEach(), or the iterator methods values(), keys() and entries().
Because a Set has no keys, the designers made its methods look like those of a Map so that code can treat both alike. That is why a Set has keys() and entries() even though they carry no extra information.
const set = new Set(["a", "b"]); for (const value of set) { console.log(value); } set.forEach((value) => console.log(value));
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
set[Symbol.iterator]() // same function as set.values() set.values() set.keys() // same function as values() set.entries() set.forEach(callbackFn) set.forEach(callbackFn, thisArg)
| Member | Description |
|---|---|
values() |
Returns an iterator over the values in insertion order. |
keys() |
An alias of values(). In a Set, the "key" and the value are the same thing. |
entries() |
Returns an iterator of [value, value] pairs, to match the shape of Map.prototype.entries(). |
[Symbol.iterator]() |
The same function as values(). This is what for...of and the spread operator call. |
forEach(callbackFn, thisArg) |
Calls callbackFn(value, value, set) once per value. The second argument repeats the first. thisArg becomes this in a regular function callback. |
Return value: values(), keys() and entries() return a new iterator object; forEach() returns undefined.
Usage notes
- Order is insertion order. Adding a value that is already present does not move it. Deleting a value and adding it again puts it at the end.
- The
forEach()callback gets the value twice (as the first and second argument) so that the same callback works withMap.prototype.forEach(). Most callbacks only declare the first parameter. - Live iteration. Values added during a loop are visited. Values deleted before the loop reaches them are skipped. A value that is deleted and re-added during the loop can be visited again, so avoid that pattern or loop over a copy:
for (const v of [...set]). - Iterators are single use. Once an iterator is exhausted, it stays exhausted. Call
values()again for a fresh one. - No index. If you need positions, spread into an array first, or use
[...set].entries(). breakandreturninsidefor...ofstop the loop early;forEach()cannot be stopped except by throwing.- Async work:
forEach()ignores the promises an async callback returns. Usefor...ofwithawait, orPromise.all([...set].map(...)). - Converting:
Array.from(set)and[...set]both make an array;Array.from(set, fn)also maps in one step.
Browser support
All current browsers support Set iteration, forEach() and the iterator methods; the newer Set algebra methods are covered on their own page.