JavaScript – Convert Array into Set

To convert an Array into a Set in JavaScript, create a new set using new Set() and pass the array as argument to the constructor. It returns a new set with the unique elements of the given array.

Syntax

The syntax to convert an Array arr into a Set using Set() constructor is

new Set(arr)
ADVERTISEMENT

Examples

In the following example, we convert an Array of numbers, arr, into a Set using Set() constructor.

index.html

Since Set can store only unique elements, and if there are any duplicate elements in the given array, then the duplicates are ignored, and only unique elements make into the Set.

index.html

Conclusion

In this JavaScript Tutorial, we learned how to convert an Array into a Set in JavaScript using Set() constructor, with examples.