JavaScript Set [Symbol.iterator]() Method

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

Definition

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
CallWhat 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] = setTake the first values out of the Set.
set[Symbol.iterator]()Call the method yourself to get the iterator object.

A standard JavaScript method (ES2015) on every Set, from Set.prototype. It is stored under the special symbol key Symbol.iterator. No import required. Siblings: add(), clear(), delete(), difference(), entries(), forEach(), has(), intersection(), isDisjointFrom(), isSubsetOf(), isSupersetOf(), keys(), symmetricDifference(), union(), values().

Library declarations
JavaScript
// 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

ParameterDescription
(none)This method takes no parameters. Call it as set[Symbol.iterator]().

Return Value

Return ValueDescription
Set IteratorAn 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);
Try It Yourself

How It Works

  1. for...of. The loop asks the Set for its iterator and takes values one by one, in the order they were added.
  2. Spread. [...colors] reads all values from the same iterator and puts them in a new array.
  3. 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.

JavaScript
const set = new Set(["a", "b"]);

console.log("Type: " + typeof set[Symbol.iterator]);
console.log("Same as values: " + (set[Symbol.iterator] === set.values));

const iterator = set[Symbol.iterator]();
const step1 = iterator.next();
const step2 = iterator.next();
const step3 = iterator.next();

console.log("Step 1: " + step1.value + " " + step1.done);
console.log("Step 2: " + step2.value + " " + step2.done);
console.log("Step 3: " + step3.value + " " + step3.done);
console.log("Iterator is iterable: " + (iterator[Symbol.iterator]() === iterator));
Try It Yourself

How It Works

  1. It is a function. typeof set[Symbol.iterator] is "function", and it is the very same function as set.values.
  2. 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.
  3. 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.

JavaScript
const letters = new Set("hello");
console.log("Letters: " + Array.from(letters).join(", "));

const numbers = new Set([4, 9, 2]);
console.log("Biggest: " + Math.max(...numbers));

function isIterable(x) {
  return x != null && typeof x[Symbol.iterator] === "function";
}

console.log("Set: " + isIterable(numbers));
console.log("Array: " + isIterable([1, 2]));
console.log("Plain object: " + isIterable({ a: 1 }));
console.log("Number: " + isIterable(5));
Try It Yourself

How It Works

  1. 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.
  2. A simple test. isIterable() checks that the value is not null or undefined and that Symbol.iterator on it is a function.
  3. Results. A Set and an array are iterable. A plain object and a number are not, because they have no [Symbol.iterator]() method.

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.

ES2015 Standard since
Google Chrome 43+ · 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 30+ · Chromium based
Full support
Samsung Internet Recent versions · Android
Supported
Android WebView 43+ · Chromium based
Supported
Node.js 4+ · Server runtime
Supported
Deno Recent versions · JavaScript runtime
Supported
Bun Recent versions · JavaScript runtime
Supported
Internet Explorer No 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.

Back to the Method Index

See every JavaScript Set method in one place, grouped by what it does.

Set Method Index →

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.

10 people found this page helpful