How to convert Set to Array in JavaScript?

21 Jul, 2021
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 =                new Set(['welcome', 'to', 'GFG']);            Array.from(set);              document.write(Array.from(set));        </script>    </center></body>  </html>Output
Array.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 =                new Set(['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 =                   new Set(['GFG', 'JS']);            const array = [...set];            document.write(array);        </script>    </center></body>  </html>Output
var 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 =                   new Set(['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>            var gfgSet = new Set();            var gfgArray = [];              gfgSet.add("Geeks");            gfgSet.add("for");            // duplicate item            gfgSet.add("Geeks");              var someFunction = function(            val1, val2, setItself) {                gfgArray.push(val1);            };              gfgSet.forEach(someFunction);              document.write("Array: " + gfgArray);        </script>    </center></body>  </html>Output
<!DOCTYPE html><html>  <head>    <title>      Convert Set to Array  </title></head>  <body>    <center>        <h1 style="color:green">          GeeksforGeeks      </h1>          <script>            var gfgSet = new Set();            var gfgArray = [];              gfgSet.add("Geeks");            gfgSet.add("for");            // duplicate item            gfgSet.add("Geeks");              var someFunction = 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
JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.
javascript-array
Picked
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
How to append HTML code to a div using JavaScript ?
Difference Between PUT and PATCH Request
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Installation of Node.js on Linux
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?