Implement a Set in JavaScript
Michael Mitrakos
3 min read
Having worked across sites raking in over 50 billion website visits annually with Higglo Digital I write about tech topics and teach…
Implement a Set in JavaScript
Having worked across sites raking in over 50 billion website visits annually with Higglo Digital I write about tech topics and teach engineers to have solid foundations that will help them get ahead in their career. I also build awesome products for digital nomads — check it out!
JavaScript eBook
I’ve written an eBook on JavaScript that will take you from beginner to professional. Having been in your shoes moving to making over $200,000 per year in just a few years as a software engineer, I know exactly what it takes to get there. Check out the ebook now!
A Set is a collection of unique values, where each value is of any type, including objects. It is a built-in data structure in JavaScript, and it was introduced in ECMAScript 2015 (ES6).
A Set is a data structure that stores a collection of unique elements. It is an unordered collection, meaning that the elements are not stored in any particular order. Sets are commonly used in programming to ensure that a collection does not contain duplicate elements. In JavaScript, sets can be implemented using the built-in Set object.
One of the main features of a Set is that it does not allow duplicate values. If you try to add a value that already exists in the Set, it will be ignored. This makes Sets very useful for storing unique values, such as a list of users or a list of tags.
You can create a Set using the Set constructor, which takes an iterable object as an argument, such as an array. For example:
const names = new Set(['Alice', 'Bob', 'Charlie']);You can also create an empty Set and add values to it later using the `add` method: ```
names.add('Bob');
names.add('Charlie');To check if a value exists in a Set, you can use the `has` method:
console.log('Alice exists in the Set');
}
To remove a value from a Set, you can use the `delete`
names.delete('Alice');You can also iterate over the values in a Set using a for-of loop:
console.log(name);
}
Sets also have a `size`
names.clear();In summary, Sets are a useful data structure in JavaScript for storing unique values and performing operations such as adding, deleting, and checking for the existence of values. They are an efficient alternative to using arrays or objects for these tasks.
#### JavaScript eBook
I’ve written an eBook on JavaScript that will take you from beginner to professional. Having been in your shoes moving to making over $200,000 per year in just a few years as a software engineer, I know exactly what it takes to get there. [Check out the ebook now](https://www.mitrakos.com/ebook)!
I founded [Higglo Digital](https://higglo.io) and we can help your business crush the web game with an award-winning website and cutting-edge digital strategy. If you want to see a *beautifully designed website*, [check us out](https://higglo.io).
I also created [Wanderlust Extension](http://www.wanderlustapp.io/) to discover the most beautiful places across the world with highly curated content. Check it out!