How do you convert a Set to an array?
By FrontendPro Editorial Team Updated 9/26/2026
Answer
Two ways, both one line and equivalent in practice:
const set = new Set([3, 1, 2]);
const a = [...set]; // spread
const b = Array.from(set); // Array.from
Use spread for brevity. Array.from is worth preferring in two cases: it takes a mapping function as a second argument, and it also accepts array-likes that have a length but are not iterable.
Array.from(set, (x) => x * 2); // map while converting, no intermediate array
Order is preserved — a Set iterates in insertion order, so the result matches the order values were added, not sorted order.
Going the other way
const back = new Set(array);
The idiom this enables
Round-tripping through a Set is the standard way to remove duplicate primitives:
const unique = [...new Set([1, 2, 2, 3])]; // [1, 2, 3]
It compares with SameValueZero, so NaN deduplicates correctly and +0/-0 collapse into one. But it only works for primitives — objects compare by reference, so [...new Set([{a:1}, {a:1}])] still has two entries. Key by an id instead:
const uniqueUsers = [...new Map(users.map((u) => [u.id, u])).values()];
Why convert at all
A Set has no map, filter, sort or index access, so converting is how you reach array methods. If you only need to loop, you do not have to convert — for...of and forEach work directly on a Set, and skipping the copy avoids allocating a second structure for nothing.
