JavaScript Set entries() Method

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

Definition

set.entries() returns an iterator that gives you one [value, value] pair for every value in the Set, in the order the values were added. Each pair is an array with the same value twice.

Why twice? A Set has no separate keys, but a Map does, and the Map version of entries() gives [key, value]. The Set version copies that shape so code that works on a Map can also work on a Set.

Remember
set.entries()

new Set(["a", "b"]).entries()
  → ["a", "a"]
  → ["b", "b"]

each pair = [value, value]
order     = the order values were added
returns   = an iterator (use for...of or spread)

Syntax

Call it on a Set. It takes no arguments and is usually used with for...of.

JavaScript
const colors = new Set(["red", "green"]);

for (const [key, value] of colors.entries()) {
  console.log(key + " / " + value);
}
// red / red
// green / green
CallWhat it does
set.entries()An iterator of [value, value] pairs.
[...set.entries()]Spread the iterator into an array of pairs.
new Map(set.entries())Build a Map where every key and value is the same value.
Object.fromEntries(set.entries())Build an object whose keys and values are the Set values.

A standard JavaScript method (ES2015) on every Set, from Set.prototype. No import required. Siblings: add(), clear(), delete(), difference(), forEach(), has(), intersection(), isDisjointFrom(), isSubsetOf(), isSupersetOf(), keys(), symmetricDifference(), union(), values(), [Symbol.iterator]().

Library declarations
JavaScript
// ECMAScript 2015 (ES6)
Set.prototype.entries()

// returns: a Set Iterator of [value, value] arrays

Parameters

ParameterDescription
(none)This method takes no parameters. Call it as set.entries().

Return Value

Return ValueDescription
Set IteratorAn iterator object. Each step gives { value: [item, item], done: false }. When all values are used, it gives { value: undefined, done: true }. The iterator can be used only once.

JavaScript Examples

Example 1 — Loop Over the Pairs

Print every pair as JSON so you can see that each pair holds the same value twice.

JavaScript
const colors = new Set(["red", "green"]);

for (const entry of colors.entries()) {
  console.log(JSON.stringify(entry));
}

console.log("Pair length: " + colors.entries().next().value.length);
Try It Yourself

How It Works

  1. Ask for the entries. colors.entries() gives an iterator, and for...of reads it one pair at a time.
  2. Each pair repeats the value. JSON.stringify() shows ["red","red"]: the first item stands for the "key" and the second one for the "value", and both are the same.
  3. Peek at one pair. next().value is the first pair, an array with 2 items. A fresh entries() call starts again from the beginning.

Example 2 — Destructure the Pairs and Make a Map

Use array destructuring in the loop, and give the pairs to new Map().

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

for (const [key, value] of letters.entries()) {
  console.log(key + " " + value + " " + (key === value));
}

const map = new Map(letters.entries());

console.log("Map size: " + map.size);
console.log("Map pairs: " + JSON.stringify(Array.from(map)));
Try It Yourself

How It Works

  1. Destructure. const [key, value] takes the two items out of each pair, so you can use them as separate variables. They are always equal.
  2. Build a Map. new Map() accepts an iterable of [key, value] pairs, and entries() is exactly that.
  3. Check the Map. The Map has 2 entries, and each entry has the Set value as both its key and its value.

Example 3 — Make an Object and Use the Iterator

Turn a Set into an object with Object.fromEntries(), and read the iterator step by step.

JavaScript
const tags = new Set(["js", "css"]);

const obj = Object.fromEntries(tags.entries());
console.log("Object: " + JSON.stringify(obj));

const iterator = tags.entries();
const first = iterator.next();

console.log("First value: " + JSON.stringify(first.value));
console.log("First done: " + first.done);
iterator.next();
console.log("Third done: " + iterator.next().done);
Try It Yourself

How It Works

  1. To an object. Object.fromEntries() turns each pair into a property, so every Set value becomes a key whose value is the same text.
  2. Step by step. next() returns an object with value (the pair) and done. The first call gives ["js","js"] and done is false.
  3. The end. The second next() gives the last pair. The third one finds nothing left, so done is true.

Browser & Runtime Support

Set.prototype.entries() is part of ES2015 (ES6), together with Set itself. Every current browser and Node.js has it.

✓ ES2015 · Widely supported

set.entries()

Supported in Chrome 38+, Edge 12+, Firefox 37+, Safari 9+, Opera 25+ and Node.js 4+. Internet Explorer never got it.

ES2015 Standard since
Google Chrome 38+ · 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 25+ · Chromium based
Full support
Samsung Internet Recent versions · Android
Supported
Android WebView 38+ · 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.entries() Excellent

Bottom line: Safe to use in every current browser and in Node.js. Only Internet Explorer needs special care.

Frequently Asked Questions

It returns an iterator that gives one [value, value] pair for each value in the Set, in the order they were added. Each pair is an array of two equal items.
A Set has no keys of its own. To keep the same shape as a Map, where entries() gives [key, value], the Set version repeats the value as both key and value. This lets the same code work with both.
values() and keys() give the plain values, and they are the same method. entries() gives [value, value] pairs. For everyday looping, for (const value of set) is the simplest choice.
No. An iterator is used up after you read it to the end. Call set.entries() again to get a fresh one that starts from the first value.
Use Array.from(set.entries()) or [...set.entries()]. If you only need the values as an array, use Array.from(set) or [...set].

Next: forEach()

Learn how to run a function for every value in a Set, and what the three callback arguments mean.

forEach() tutorial →

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.

9 people found this page helpful