Could we help you? Please click the banners. We are young and desperately need the money
In JavaScript, arrays allow multiple instances of the same item to be present. To circumvent this, you can use Sets. Under the hood, they are a modification of Maps that will map values as both the key and value, thus forcing every value to be unique, because keys in Maps must be. We recommend you to read our guide on Maps first.
Here is how you declare a Set:
const set = new Set();
const set = new Set([1, 2, 3]);
And here is how you add values to it:
set.add(4);
If you try to add a value that is already included in the set, nothing will happen.
Other than that, usage is virtually identical with Maps.
Set has recently received some handy Boolean methods. They are Baseline 2024 and available across major browsers since June 2024.
Those include: