How do you convert a Set to an Array?
The short answer
The two main ways are the spread operator ([...set]) and Array.from(set). Both produce a regular array from a Set. This is commonly used to remove duplicates from an array.
The methods
1const mySet = new Set([1, 2, 3, 4, 5]);23// Spread operator (most common)4const arr1 = [...mySet]; // [1, 2, 3, 4, 5]56// Array.from7const arr2 = Array.from(mySet); // [1, 2, 3, 4, 5]
Removing duplicates from an array
This is the most practical use case:
1const numbers = [1, 2, 2, 3, 3, 3, 4];2const unique = [...new Set(numbers)]; // [1, 2, 3, 4]
One line — create a Set (which automatically removes duplicates), then spread it back into an array.
For objects, you need a different approach since Sets use reference equality:
1const users = [2 { id: 1, name: 'John' },3 { id: 2, name: 'Jane' },4 { id: 1, name: 'John' },5];67// Remove duplicates by id8const unique = [9 ...new Map(users.map((u) => [u.id, u])).values(),10];
Interview Tip
Show the spread operator and the duplicate removal one-liner. This is a quick knowledge question. The [...new Set(array)] pattern for removing duplicates is the most practical thing to mention.
Why interviewers ask this
This tests knowledge of Sets and array conversion. The duplicate removal pattern is something you use in real code, and knowing it shows practical JavaScript skills.