set.values() returns an iterator that gives you every value of the Set, one by one, in the order the values were added. It is the method that for (const value of set) uses behind the scenes.
The iterator is not an array. To get an array, wrap it in Array.from() or spread it with [...set.values()]. The iterator is live and can be used only once.
Remember
set.values()
gives every value, in the order they were added
returns an iterator, not an array
Array.from(set.values()) → array
[...set.values()] → array
An iterator can be used only once
Syntax
Call it on a Set. It takes no arguments and is usually used with for...of or Array.from().
JavaScript
const colors = new Set(["red", "green", "blue"]);
for (const value of colors.values()) {
console.log(value);
}
// red
// green
// blue
Call
What it does
set.values()
An iterator of the values of the Set.
Array.from(set.values())
Turn the values into an array.
Math.max(...set.values())
Spread the values into a function call, for example to find the biggest number.
for (const value of set)
The shortest way to loop. It uses the same iterator.
// ECMAScript 2015 (ES6)
Set.prototype.values()
// returns: a Set Iterator of the values
Parameters
Parameter
Description
(none)
This method takes no parameters. Call it as set.values().
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 Values
Use values() in a for...of loop, and check that the default loop gives the same values.
JavaScript
const colors = new Set(["red", "green", "blue"]);
for (const value of colors.values()) {
console.log("Value: " + value);
}
const plain = [];
for (const value of colors) {
plain.push(value);
}
console.log("Plain loop: " + plain.join(", "));
Read one value. The first next() gives 1. The iterator now waits at 2.
Add a value.set.add(3) adds a value at the end. Because the iterator is live, it reaches 3 too, so the rest is 2, 3.
Used up. The old iterator has no values left, so it gives an empty array. A fresh set.values() starts again and finds all 3 values.
Compatibility
Browser & Runtime Support
Set.prototype.values() is part of ES2015 (ES6), together with Set itself. Every current browser and Node.js has it.
✓ ES2015 · Widely supported
set.values()
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.values()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, one at a time, in the order they were added. It is not an array.
Use Array.from(set.values()), [...set.values()], or the shorter Array.from(set) and [...set]. All of them give the values in insertion order.
There is none for a Set. Set.prototype.keys and Set.prototype.values are the same function. values() is the clearer name for a Set.
No. After the last value has been read, the iterator stays finished. Call set.values() again to get a new iterator that starts from the first value.
No. for (const value of set) already uses the same iterator. Use values() when you need an iterator object itself, for example with next(), or to pass to another function.