Sets - My Favorite ES6 Feature! 🚗
![](https://substackcdn.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F02364487-ba56-41bf-b776-ba99fc237f9a_1200x400.png)
Hi, *|USERNAME|*!
Arrays have been around forever. They are kinda like the tardigrades of data structures. A part of the reason arrays are so well-established in our programming world has to do with their flexibility. If you are looking to store a collection of data, nothing beats the array in terms of functionality it provides to make all of this collection data wrangling straightforward. Over the years, more specialized data structures have come about that added some additional bells and whistles over what our arrays provided. One such array-like data structure is the Set:
let mySet = new Set();
mySet.add("blarg");
mySet.add(10);
mySet.add(true);
mySet.add("blarg");
mySet.add("blarg");
console.log(mySet); // "blarg", 10, true
Sets and arrays are very similar, but there is one big thing that sets sets (ha!) apart. Sets don't store duplicate values. In other words, we can use a set to store whatever we want (like an array), but we can store that item only once (unlike an array). In the above snippet, notice that we are adding a bunch of values to our Set object called mySet. The word blarg is being added three times. Because duplicates aren't allowed, when we inspect the contents of mySet, the word blarg shows up only once. Uniqueness FTW!
Before we had sets, if we wanted to store a collection of unique data, we had to add a lot of code to make our arrays behave the way we wanted. With the Set object, which made its appearance as part of ES6, we no longer have to do that. To learn more about sets, check out the Diving Into Sets tutorial that was added to the site recently. You can also take the quick two-question Sets Quiz to see if you get the gist of what sets are all about.
TILL NEXT TIME!
As always, before I leave you to it, I would love to hear from you on things I can do better, topics you'd like me to write about, and more. Don't be shy! The easiest way to contact me is via Twitter or by posting on the forums (like you may have read about in history books)!
Cheers,
Kirupa 😃