How to convert Set to Array in JavaScript?
A set can be converted to an array in JavaScript by the following way-
- By using Array.from() method:
This method returns a new Array from an array like object or iterable objects like Map, Set, etc.
SyntaxArray.from(arrayLike object);
Example-1
<!DOCTYPE html><html><head><title>Convert Set to Array</title></head><body><center><h1 style="color:green">GeeksforGeeks</h1><script>const set =newSet(['welcome','to','GFG']);Array.from(set);document.write(Array.from(set));</script></center></body></html>Output

- Using spread operator:
Using of spread operator can also help us convert Set to array.
Syntaxvar variablename = [...value];
Example-2:
<!DOCTYPE html><html><head><title>Convert Set to Array</title></head><body><center><h1 style="color:green">GeeksforGeeks</h1><script>const set =newSet(['GFG','JS']);const array = [...set];document.write(array);</script></center></body></html>Output

- Using forEach:
Example-3:<!DOCTYPE html><html><head><title>Convert Set to Array</title></head><body><center><h1 style="color:green">GeeksforGeeks</h1><script>vargfgSet =newSet();vargfgArray = [];gfgSet.add("Geeks");gfgSet.add("for");// duplicate itemgfgSet.add("Geeks");varsomeFunction =function(val1, val2, setItself) {gfgArray.push(val1);};gfgSet.forEach(someFunction);document.write("Array: "+ gfgArray);</script></center></body></html>Output

Supported Browsers:
- Google Chrome
- Firefox
- Edge
- Opera
- Apple Safari



