set.keys() returns an iterator that gives you every value of the Set, in the order the values were added. It works like values(): in fact, Set.prototype.keysis the same function as Set.prototype.values.
Why does a Set have "keys" at all? A Set has no separate keys, but a Map does. By giving a Set a keys() method too, code that loops over collection.keys() can work with both a Map and a Set.
Remember
set.keys()
gives every value, in the order they were added
is the same method as set.values()
Set.prototype.keys === Set.prototype.values → true
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", "blue"]);
for (const key of colors.keys()) {
console.log(key);
}
// red
// green
// blue
Call
What it does
set.keys()
An iterator of the values of the Set.
[...set.keys()]
Spread the iterator into an array.
set.values()
The same iterator. This name is clearer for a Set.
for (const value of set)
The shortest way to loop, and it uses the same iterator.
// ECMAScript 2015 (ES6)
Set.prototype.keys()
// returns: a Set Iterator of the values
// Set.prototype.keys is the same function as Set.prototype.values
Parameters
Parameter
Description
(none)
This method takes no parameters. Call it as set.keys().
Return Value
Return Value
Description
Set Iterator
An iterator object. Each step gives { value: 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 Keys
Use keys() in a for...of loop, and then read the iterator by hand with next().
JavaScript
const colors = new Set(["red", "green", "blue"]);
for (const key of colors.keys()) {
console.log("Key: " + key);
}
const iterator = colors.keys();
console.log("First: " + iterator.next().value);
console.log("Second: " + iterator.next().value);
One function.listKeys() only needs an object with a keys() method, so it does not care what type it gets.
A Map. The Map gives its real keys, a and b.
A Set. The Set gives its values x and y. This is why Set has keys(): it makes sharing code like this possible.
Compatibility
Browser & Runtime Support
Set.prototype.keys() is part of ES2015 (ES6), together with Set itself. Every current browser and Node.js has it.
✓ ES2015 · Widely supported
set.keys()
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.keys()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 every value of the Set, in the order they were added. A Set has no separate keys, so the keys are the same as the values.
There is none. Set.prototype.keys and Set.prototype.values are the same function. keys() exists so a Set can be used where code expects a Map-like object.
To match Map. Code that calls keys() on a collection can then work with both a Map and a Set without checking the type first.
Use values(), or simply for (const value of set), because the names say what you get. Use keys() only when the same code must also accept a Map.
Use Array.from(set.keys()) or [...set.keys()]. Both give the same result as Array.from(set) and [...set].