Let's begin with Sets. In JavaScript, a Set is a unique type of object that stores various data types, either primitive or objects. A Set does not contain duplicates. Think of a Set as a cosmic bag holding celestial bodies (elements), ensuring that each remains one-of-a-kind.
The array
includes duplicates. The set
, in contrast, displays only unique values.
The creation of a Set involves the new
keyword and the Set()
method. You can add items using the .add()
method.
In this case, 'Star'
was added twice, but the Set retains it as a single entry to prevent duplication.
To remove entries from a Set, use the .delete()
method. If you want to clear a Set entirely, use the method.
