Creating a Set
suggest changeThe 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.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents