Articles by FavTutor
  • AI News
  • Data Structures
  • Web Developement
  • AI Code GeneratorNEW
  • Student Help
  • Main Website
No Result
View All Result
FavTutor
  • AI News
  • Data Structures
  • Web Developement
  • AI Code GeneratorNEW
  • Student Help
  • Main Website
No Result
View All Result
Articles by FavTutor
No Result
View All Result
Home Web Developement

Convert Set to Array in JavaScript (3 Easy Methods)

Komal Bhatia by Komal Bhatia
November 14, 2023
Reading Time: 7 mins read
Converting set to array in javascript
Follow us on Google News   Subscribe to our newsletter

JavaScript offers a powerful data structure called Set, which allows for the storage of unique elements without duplication. However, there are situations where you may need to convert them to an Array to perform specific operations that cannot be done directly on a Set. In this article, we will learn how to change Set to Array in JavaScript.

What are Arrays in JavaScript?

Arrays are linear data structures that store homogenous data, i.e. same type of data. An array can contain all integers, all characters, or all strings as the elements. An array can not be a combination of integers and characters, or integers or strings. Let us look at how to declare and use arrays in Javascript:

//declaring an integers array
let marks = [98, 75, 62, 82, 97, 53];

//declaring a characters array
let grades=['A', 'B', 'A', '0'];

//declaring a strings array
let favtutors=["John", "Joshua", "Alex", "Kate"];

//declaring a boolean array
let present=[true, false, false, true];

In the code above, we have made integers array marks containing all integers, a characters array grade consisting of all elements as characters, a strings array ‘favtutors’ consisting of all strings, and a boolean present array consisting of all boolean values.

You can check out this article if you want to learn how to Convert a String to an Array in JavaScript.

What are Sets in JavaScript?

A set is a data type that stores unique values. No duplicate values are found in a set. It can hold either unique integers, unique characters, or unique strings. In a set, one cannot access any element with the help of an index. Here’s an example of how to create an empty set in JavaScript:

//creating an empty set in JavaScript
let ids=new Set();

//printing the empty set
console.log(ids);

Output:

Set(0) { }

Here 0 represents the number of values in the set. The { } represents the empty set.

You can also create a set with some unique values:

//creating a set with some values
let ids=new Set([1,2,3]);

//printing the set
console.log(ids);

Output:

Set(3) { 1, 2, 3 } 

Here 3 represents the number of elements in the set. We have obtained the desired result.

3 Methods to Convert Set to Array

In this section, we will discuss various techniques on how to convert a set to array in JavaScript:

1) Using the Array.from() Method

The Array.from() method is a built-in function in JavaScript that creates a new Array from an iterable or array-like object. It takes an iterable or array-like object as its parameter. It returns a new Array with the same elements as the input iterable object. This method supports the mapping function for element transformation and provides high clarity and readability.

To create a set from an array, simply make a set with desired elements and pass it as a parameter in Array.from() method. This will convert the set to an array. Store the array in a new variable. Let us see an example of the same:

//creating a set
let ids=new Set([1,2,3,4,5]);

//printing the set
console.log(ids);

//converting set to an array using Array.from() method
let arrFromSet=Array.from(ids);

//printing the array
console.log(arrFromSet);

Output:

Set(5) { 1, 2, 3, 4, 5 }
[ 1, 2, 3, 4, 5 ]

In this example, we convert the set to an array using the Array.from() method. We have first printed the set and then the array which is formed from the set itself.  We have obtained the desired output. 

Although this method is simple, clear, readable, and efficient, it requires more code than the spread operator. Let us learn how to use the spread operator.

2) Using the Spread Operator

Another method of converting set to array in JavaScript is by using the spread operator (…). The spread operator allows us to expand elements from an iterable object, such as a Set, into a new Array. This method is concise and straightforward to implement.

To create a set from an array, simply make a set with desired elements. Use the spread operator (…) followed by the Set name inside square brackets to spread the elements into a new Array. Finally, store the array in a new variable. Let us see an example of the same:

//creating a set
let ids=new Set([101, 102, 103]);

//printing the set
console.log(ids);

//converting set to an array using the spread operator
let arrFromSet=[...ids];

//printing the array
console.log(arrFromSet);

Output:

Set(3) { 101, 102, 103 }
[ 101, 102, 103 ]

In this example, we convert the set to an array using the spread operator. We have first printed the set and then the array which is formed from the set itself.  We have obtained the desired output. 

Although this method is concise and easy to use, it does not support the mapping function during conversion.

3) Using the forEach() Method

Another method for converting a set to an array is the forEach() method. The forEach() is a built-in function in JavaScript that allows you to execute a provided function once for each element in an array or Set. We can iterate over the elements of a set using the forEach() method and push them into a new Array.

So, firstly create a Set with the desired elements. Use the forEach() method on the Set to iterate over it and provide a function that pushes each element into the Array. Store the array into a new variable. Let us see an example of the same.

//creating a set
let ids=new Set([101, 102, 103]);

//printing the set
console.log(ids);

//declaring an empty array
let arrFromSet=[];

//using the forEach() method
ids.forEach(element=>arrFromSet.push(element));

//printing the array
console.log(arrFromSet);

Output:

Set(3) { 101, 102, 103 }
[ 101, 102, 103 ]

In this example, we convert the set to array using the forEach() method. We have first printed the set and then the array which is formed from the set itself.  We have obtained the desired output.

This method allows greater control during conversion as we are able to perform additional operations on each element. But, Unlike previous methods, we have to perform the push operation in an array manually.

Comparison of the Three Methods

Now that we have gone through various methods of converting a set to an array in JavaScript, it’s time we compare them based on different factors.

MethodComplexitySupport for Mapping FunctionClarity and Readability
Array.from()O(n)YesHigh
Spread OperatorO(n)NoHigh
forEach() MethodO(n)NoMedium

The choice of method depends on your specific requirements and coding style. 

  • If one needs to apply a mapping function to each element during conversion, the Array.from() method is preferred.
  • The spread operator is chosen when simplicity and conciseness are important.
  • When one needs more control over each element to apply different operations, forEach() method is preferred.

Conclusion

In this article, we discussed the various techniques to convert a JavaScript Set to Array, using Array.from(), forEach() method, and the spread operator. We also provided a comparison of the methods along with their time complexities. 

ShareTweetShareSendSend
Komal Bhatia

Komal Bhatia

I am a dedicated Computer Science student with a strong track record of academic excellence. I am a highly motivated individual with a passion for providing creative and innovative solutions to complex problems. I possess skills in the domain of C++ and web development and am always eager to contribute to the field of Software Development.

RelatedPosts

Javascript Animation Libraries

Top 10 JavaScript Animation Libraries in 2025

February 19, 2025
JavaScript Interview Questions

Top 40 JavaScript Interview Questions and Answers in 2024

April 1, 2025
Best React UI Component Libraries

10 Must-Know React UI Component Libraries for Web Devs 2024

May 7, 2024
Currying in JavaScript

Currying in JavaScript Explained (with Examples)

March 15, 2024
Javascript Format Currency

Javascript Program for Format Currency

April 8, 2025

About FavTutor

FavTutor is a trusted online tutoring service to connects students with expert tutors to provide guidance on Computer Science subjects like Java, Python, C, C++, SQL, Data Science, Statistics, etc.

Categories

  • AI News, Research & Latest Updates
  • Trending
  • Data Structures
  • Web Developement
  • Data Science

Important Subjects

  • Python Assignment Help
  • C++ Help
  • R Programming Help
  • Java Homework Help
  • Programming Help

Resources

  • About Us
  • Contact Us
  • Editorial Policy
  • Privacy Policy
  • Terms and Conditions

Website listed on Ecomswap. © Copyright 2025 All Rights Reserved.

No Result
View All Result
  • AI News
  • Data Structures
  • Web Developement
  • AI Code Generator
  • Student Help
  • Main Website

Website listed on Ecomswap. © Copyright 2025 All Rights Reserved.