set[Symbol.iterator]() is the special method that makes a Set iterable. JavaScript calls it for you whenever you use a Set with for...of, the spread syntax [...set], array destructuring, Array.from(set) or new Set(iterable).
It returns an iterator that gives the values of the Set in the order they were added. For a Set it is the same function as values() and keys(), so you will rarely call it by hand.
Remember
set[Symbol.iterator]()
called for you by: for...of, [...set],
const [a, b] = set,
Array.from(set), new Map(set) ...
Set.prototype[Symbol.iterator] === Set.prototype.values → true
returns an iterator of the values
Syntax
You normally use it through for...of or spread. You can also call it directly with square brackets.
JavaScript
const colors = new Set(["red", "green"]);
// JavaScript calls it for you here:
for (const color of colors) {
console.log(color);
}
// ...or you can call it yourself:
const iterator = colors[Symbol.iterator]();
console.log(iterator.next().value); // red
Call
What it does
for (const value of set)
Loop over the values. The loop calls [Symbol.iterator]() for you.
[...set]
Spread the values into a new array.
const [first, second] = set
Take the first values out of the Set.
set[Symbol.iterator]()
Call the method yourself to get the iterator object.
// ECMAScript 2015 (ES6)
Set.prototype[Symbol.iterator]()
// returns: a Set Iterator of the values
// Set.prototype[Symbol.iterator] is the same function as Set.prototype.values
Parameters
Parameter
Description
(none)
This method takes no parameters. Call it as set[Symbol.iterator]().
Return Value
Return Value
Description
Set Iterator
An iterator object with a next() method. Each step gives { value: item, done: false }, and the last step gives { value: undefined, done: true }. The iterator itself is iterable, so it can be used in for...of too.
JavaScript Examples
Example 1 — for...of, Spread and Destructuring
Three everyday features that use [Symbol.iterator]() for you.
JavaScript
const colors = new Set(["red", "green", "blue"]);
for (const color of colors) {
console.log("Loop: " + color);
}
const list = [...colors];
console.log("Spread: " + list.join(", "));
const [first, second] = colors;
console.log("First two: " + first + ", " + second);
Loop: red
Loop: green
Loop: blue
Spread: red, green, blue
First two: red, green
How It Works
for...of. The loop asks the Set for its iterator and takes values one by one, in the order they were added.
Spread.[...colors] reads all values from the same iterator and puts them in a new array.
Destructuring.const [first, second] = colors takes only the first two values. A Set has no index like colors[0], so this is a handy way to read the first values.
Example 2 — Call It Yourself
Call the method with square brackets and step through the iterator by hand.
Type: function
Same as values: true
Step 1: a false
Step 2: b false
Step 3: undefined true
Iterator is iterable: true
How It Works
It is a function.typeof set[Symbol.iterator] is "function", and it is the very same function as set.values.
Step through. Each next() gives an object with value and done. After a and b, the third call has no value and done is true.
Iterators are iterable too. Calling [Symbol.iterator]() on the iterator returns the iterator itself, which is why you can put an iterator inside for...of.
Example 3 — Who Uses It? Check for Iterables
Several built-in features accept any iterable. A small helper checks if a value is iterable by looking for this method.
Letters: h, e, l, o
Biggest: 9
Set: true
Array: true
Plain object: false
Number: false
How It Works
Any iterable works.new Set("hello") reads the string one letter at a time and keeps each letter once. Math.max(...numbers) spreads the Set into arguments.
A simple test.isIterable() checks that the value is not null or undefined and that Symbol.iterator on it is a function.
Results. A Set and an array are iterable. A plain object and a number are not, because they have no [Symbol.iterator]() method.
Compatibility
Browser & Runtime Support
Set.prototype[Symbol.iterator]() is part of ES2015 (ES6), together with Set itself. Every current browser and Node.js has it.
✓ ES2015 · Widely supported
set[Symbol.iterator]()
Supported in Chrome 43+, Edge 12+, Firefox 37+, Safari 9+, Opera 30+ and Node.js 4+. Internet Explorer never got it.
ES2015Standard since
Google Chrome43+ · Desktop & Mobile
Full support
Microsoft Edge12+ · Chromium based
Full support
Mozilla Firefox37+ · Desktop & Mobile
Full support
Apple Safari9+ · macOS & iOS
Full support
Opera30+ · Chromium based
Full support
Samsung InternetRecent versions · Android
Supported
Android WebView43+ · Chromium based
Supported
Node.js4+ · Server runtime
Supported
DenoRecent versions · JavaScript runtime
Supported
BunRecent versions · JavaScript runtime
Supported
Internet ExplorerNo native support
Not supported
set[Symbol.iterator]()Excellent
Bottom line: Safe to use in every current browser and in Node.js. Only Internet Explorer needs special care.
Frequently Asked Questions
It is the special method that makes a Set iterable. JavaScript calls it automatically for for...of, spread, destructuring and Array.from(). It returns an iterator of the values of the Set.
Yes. Set.prototype[Symbol.iterator] is the same function as Set.prototype.values, and keys() is the same function too. All three give the values in insertion order.
Use square brackets with the symbol: const iterator = set[Symbol.iterator](). Then call iterator.next() to get the values one by one. Most of the time for...of is easier.
It means that the Set has a [Symbol.iterator]() method, so features such as for...of and the spread syntax know how to read its values one at a time.
A Set is not an array and has no positions you can read with numbers. To read values by position, spread the Set into an array first with [...set], or take the first values with destructuring.