The Set keys() Method
Example
// Create a Set
const letters = new Set(["a","b","c"]);
// Get the Values
const myIterator = letters.keys();
// List the Values
let text = "";
for (const x of myIterator) {
text += x;
}
Try it Yourself »const letters = new Set(["a","b","c"]);
// Get the Values
const myIterator = letters.keys();
// List the Values
let text = "";
for (const x of myIterator) {
text += x;
}
More Examples Below !
Description
Thekeys() method returns an Iterator object with the values in a set.
Thekeys() method does not change the original set.
Note
Since a set has no keys, thekeys() method returns the same asvalues().
This makes JavaScript sets compatible with JavaScript maps.
Syntax
set.keys()
Parameters
| NONE |
Return Value
| Type | Description |
| Iterator | An iterable object with the values of the set. |
Related Set Methods:
See Also:
More Examples
Looping the set.keys() directly:
// Create a Set
const letters = new Set(["a","b","c"]);
// List all Elements
let text = "";
for (const x of letters.keys()) {
text += x;
}
Try it Yourself »const letters = new Set(["a","b","c"]);
// List all Elements
let text = "";
for (const x of letters.keys()) {
text += x;
}
Browser Support
set.keys() is an ECMAScript6 (ES6 2015) feature.
JavaScript 2015 is supported in all browsers sinceJune 2017:
| Chrome 51 | Edge 15 | Firefox 54 | Safari 10 | Opera 38 |
| May 2016 | Apr 2017 | Jun 2017 | Sep 2016 | Jun 2016 |

