JavaScript Set keys() Method

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

Definition

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.keys is 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
CallWhat 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.

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

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

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

Return Value

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

How It Works

  1. Loop. for...of reads the iterator from keys() and gives each value in the order it was added.
  2. A new iterator. The second keys() call starts again from the beginning. Each next() moves to the next value.
  3. The key is the value. The word "key" is only a name here. What you get is the same as what you stored.

Example 2 — keys() Is the Same as values()

Compare keys() with values(), and with the default iterator that for...of uses.

JavaScript
const set = new Set([1, 2, 3]);

console.log("Same method: " + (Set.prototype.keys === Set.prototype.values));
console.log("Default iterator: " + (Set.prototype[Symbol.iterator] === Set.prototype.values));

const fromKeys = Array.from(set.keys());
const fromValues = Array.from(set.values());

console.log("Keys: " + fromKeys.join(", "));
console.log("Values: " + fromValues.join(", "));
console.log("Equal: " + (fromKeys.join() === fromValues.join()));
Try It Yourself

How It Works

  1. One function, three names. keys, values and Symbol.iterator all point to the very same function on Set.prototype.
  2. Same data. Array.from() turns each iterator into an array, so you can see the results side by side.
  3. Same answer. Both arrays hold 1, 2, 3, so join() gives the same text and the last line is true.

Example 3 — Share Code Between a Map and a Set

One small function that lists the keys of anything that has keys(). It works for a Map and for a Set.

JavaScript
function listKeys(collection) {
  return Array.from(collection.keys()).join(", ");
}

const map = new Map([["a", 1], ["b", 2]]);
const set = new Set(["x", "y"]);

console.log("Map: " + listKeys(map));
console.log("Set: " + listKeys(set));
Try It Yourself

How It Works

  1. One function. listKeys() only needs an object with a keys() method, so it does not care what type it gets.
  2. A Map. The Map gives its real keys, a and b.
  3. A Set. The Set gives its values x and y. This is why Set has keys(): it makes sharing code like this possible.

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.

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.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].

Next: symmetricDifference()

Learn how to get the values that are in only one of two Sets, as a new Set.

symmetricDifference() 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.

8 people found this page helpful