Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
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,696 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?