How do you convert a Set to an Array?

JavaScript

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

const mySet = new Set([1, 2, 3, 4, 5]);
// Spread operator (most common)
const arr1 = [...mySet]; // [1, 2, 3, 4, 5]
// Array.from
const arr2 = Array.from(mySet); // [1, 2, 3, 4, 5]

Removing duplicates from an array

This is the most practical use case:

const numbers = [1, 2, 2, 3, 3, 3, 4];
const 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:

const users = [
{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' },
{ id: 1, name: 'John' },
];
// Remove duplicates by id
const unique = [
...new Map(users.map((u) => [u.id, u])).values(),
];

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.