Creating a Set

suggest change

The Set object lets you store unique values of any type, whether primitive values or object references.

You can add items to a set and iterate them much as you would an array, but a Set will not store a value that it already contains.

To create a new set:

const mySet = new Set();

Or you can create a set from any iterable object to give it starting values:

const arr = [1,2,3,4,4,5];
const mySet = new Set(arr);

In the example above the set content would be {1, 2, 3, 4, 5}. Note that the value 4 appears only once, unlike in the original array used to create it.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents