Why use Set at all?
What are the advantages of using Set instead of just using arrays or objects? I can't seem to find any real-world examples of how using Set is a better option...
Christian Andersson8,508 Points
One of the biggest differences of a Set is that it only allows unique values - you cannot have duplicates - which can be an advantage in certain cases.
Sets also have convenient methods, such as
has() to find out if an element exists or not. It is also an
iterable object which can help iterate over the element in the object.
There are other objects that are more common and offer similar features, such as
Set specifically has the benefit of the unique-elements requirement, which would save the headache of checking-before-inserting that you would otherwise have to do with, say, Arrays.
If you don't need this requirement, I would suggest using a Map or some other Object - perhaps even a simple Array.
Gotcha, thank you!
Greg Schudel4,090 Points
The Set specifically has the benefit of the unique-elements requirement, which would save the headache of checking->before-inserting that you would otherwise have to do with, say, Arrays.
what is checking-before-inserting? you mean running a console.log check to see what the interpreter is actually processing? Can you provide an example of what you mean please?