w3docs logo
  1. Snippets
  2. JavaScript
  3. How to Convert Set to Array in JavaScript

How to Convert Set to Array in JavaScript

You can either use Array or Set to get an optimal result. However, sometimes, you need to convert Set to Array. In this tutorial, you will find several JavaScript methods of converting Set to Array easily.

Array.from()

The Array.from() method returns a new Array from an array-like object or iterable objects such as Map, Set:

w3docs logo Javascript convert set to array use the Array.from method
const set = new Set(['welcome', 'to', 'W3docs']); console.log(Array.from(set));

The spread Operator

You can spread the Set out in an Array like this:

w3docs logo Javascript convert set to array use the spread operator
const set = new Set(['welcome', 'to', 'W3docs']); let array = [...set]; console.log(array);

forEach

Here is the old fashion way, iterating and pushing to a new Array:

w3docs logo Javascript convert set to array iterating and push to a new array
const set = new Set(['welcome', 'to', 'W3docs']); let array = []; set.forEach(v => array.push(v)); console.log(array);

An Array is a structure which represents the block of data (numbers, objects, etc.) allocated in consecutive memory. Set is an abstract data type which contains distinct elements without the need of being allocated orderly by index. Elements in Array can be duplicated, and in Set, the elements can’t.

Do you find this helpful?