JavaScript – Create a New Set

To create a new Set in JavaScript, use Set() constructor. Set() constructor accepts an array of elements, and creates a set from this array. If no argument is passed to Set() constructor, then it returns an empty set.

Syntax

The syntax to create a Set using Set() constructor is

new Set() //empty set
new Set([element1, element2, ..., elementN]) //set from array
ADVERTISEMENT

Examples

In the following example, we create an empty set, and add some elements to it.

index.html

In the following example, we create a set with by passing an array of elements as argument to Set() constructor.

index.html

Conclusion

In this JavaScript Tutorial, we learned how to create a Set in JavaScript using Set() constructor, with examples.