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
Call
What 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.
// ECMAScript 2015 (ES6)
Set.prototype.entries()
// returns: a Set Iterator of [value, value] arrays
Parameters
Parameter
Description
(none)
This method takes no parameters. Call it as set.entries().
Return Value
Return Value
Description
Set Iterator
An 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);
Ask for the entries.colors.entries() gives an iterator, and for...of reads it one pair at a time.
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.
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)));
Object: {"js":"js","css":"css"}
First value: ["js","js"]
First done: false
Third done: true
How It Works
To an object.Object.fromEntries() turns each pair into a property, so every Set value becomes a key whose value is the same text.
Step by step.next() returns an object with value (the pair) and done. The first call gives ["js","js"] and done is false.
The end. The second next() gives the last pair. The third one finds nothing left, so done is true.
Compatibility
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.
ES2015Standard since
Google Chrome38+ · Desktop & Mobile
Full support
Microsoft Edge12+ · Chromium based
Full support
Mozilla Firefox37+ · Desktop & Mobile
Full support
Apple Safari9+ · macOS & iOS
Full support
Opera25+ · Chromium based
Full support
Samsung InternetRecent versions · Android
Supported
Android WebView38+ · 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.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].