How to use the Javascript Set() ES6 method — 100daysofleetcode epiphany
An overview of the Set() method in Javascript ES6

I am a Designer and Front End developer creating beautiful experiences online
Search for a command to run...
An overview of the Set() method in Javascript ES6

I am a Designer and Front End developer creating beautiful experiences online
No comments yet. Be the first to comment.
An introduction to Definite Assignment Assertions in Typescript.

Converting your object literals to the typescript side

tl:dr — run expo init ‘project_name’ --npm

Using environment variables when working with reactjs template inside Vite

I recently started a #100DaysofLeetcode challenge and came across a coding problem whose solution involved using the Javascript ES6 Set() method. I dug deeper into what it was, how it works and how to use it in your day to day coding and this article will try to shed some more light into it.
The Set() method is used to store unique values of any type, whether primitive values or object references. You can iterate through all elements in a set and values inside a set may only occur once.
Sets may look similar to Arrays but some key differences include:
When creating a set, we write new Set() which creates a new empty Set.
const blogTags = new Set();
You can also pass in an iterable object whose values will be added to new Set created
const blogTags = new Set(["HTML", "Productivity"]);
With our created Set, we can now add more items, this is done with the add() method as shown below
const blogTags = new Set();
blogTags.add("Productivity");
blogTags.add("CSS");
output
Set(2){"Productivity","CSS"}
trying to add a duplicate value to the newly created Set will be ignored.
A set doesn't support random access but you can search for specific items inside the set with has()
const blogTags = new Set();
blogTags.add("Productivity");
blogTags.add("CSS")
blogTags.has("CSS"); //true
blogTags.has("Javascript"); //false
To remove a single value from the set you use delete()
const blogTags = new Set();
blogTags.add("Productivity");
blogTags.add("CSS");
blogTags.has("CSS"); //true
blogTags.delete("CSS");
blogTags.has("CSS"); //false
Since sets are iterable objects, we can loop through them and also use the spread operator. Values inside a set are arranged in the order by which they were inserted inside the set created
const blogTags = new Set();
blogTags.add("Productivity");
blogTags.add("CSS");
blogTags.add("Devops");
for(let tag of blogTags){
console.log("Tags in the blog include", tag);
}
output
Tags in the blog include Productivity
Tags in the blog include CSS
Tags in the blog include Devops
Sets are not a replacement for arrays or other data structures but are useful for some specific situations including:
Conclusion
I jumped into the #100daysofleetcode challenge with a goal of being good at Data structures and Algorithms for interviews, but later realized that I'm learning alot of Javascript along the way. I've added more resources you can use to dive deep into the workings of the Set() method. Hope this helped and happy Coding!!
— Extra resources and deep dive