The Set JavaScript Data Structure
A Set data structure allows to add data to a container, a collection of objects or primitive types (strings, numbers or booleans), and you can think of it as a Map where values are used as map keys, with the map value always being a boolean true.
What is a Set
A Set data structure allows to add data to a container.
ECMAScript 6 (also called ES2015) introduced the Set data structure to the JavaScript world, along with Map
A Set is a collection of objects or primitive types (strings, numbers or booleans), and you can think of it as a Map where values are used as map keys, with the map value always being a boolean true.
Initialize a Set
A Set is initialized by calling:
const s = new Set()
Add items to a Set
You can add items to the Set by using the add method:
s.add('one')
s.add('two')
A set only stores unique elements, so calling s.add('one') multiple times won’t add new items.
You can’t add multiple elements to a set at the same time. You need to call add() multiple times.
Check if an item is in the set
Once an element is in the set, we can check if the set contains it:
s.has('one') //true
s.has('three') //false
Delete an item from a Set by key
Use the delete() method:
s.delete('one')
Determine the number of items in a Set
Use the size property:
s.size
Delete all items from a Set
Use the clear() method:
s.clear()
Iterate the items in a Set
Use the keys() or values() methods - they are equivalent:
for (const k of s.keys()) {
console.log(k)
}
for (const k of s.values()) {
console.log(k)
}
The entries() method returns an iterator, which you can use like this:
const i = s.entries()
console.log(i.next())
calling i.next() will return each element as a { value, done = false } object until the iterator ends, at which point done is true.
You can also use the forEach() method on the set:
s.forEach(v => console.log(v))
or you can just use the set in a for..of loop:
for (const k of s) {
console.log(k)
}
Initialize a Set with values
You can initialize a Set with a set of values:
const s = new Set([1, 2, 3, 4])
Convert to array
Convert the Set keys into an array
const a = [...s.keys()]
// or
const a = [...s.values()]
A WeakSet
A WeakSet is a special kind of Set.
In a Set, items are never garbage collected. A WeakSet instead lets all its items be freely garbage collected. Every key of a WeakSet is an object. When the reference to this object is lost, the value can be garbage collected.
Here are the main differences:
- you cannot iterate over the WeakSet
- you cannot clear all items from a WeakSet
- you cannot check its size
A WeakSet is generally used by framework-level code, and only exposes these methods:
- add()
- has()
- delete()
download all my books for free
- javascript handbook
- typescript handbook
- css handbook
- node.js handbook
- astro handbook
- html handbook
- next.js pages router handbook
- alpine.js handbook
- htmx handbook
- react handbook
- sql handbook
- git cheat sheet
- laravel handbook
- express handbook
- swift handbook
- go handbook
- php handbook
- python handbook
- cli handbook
- c handbook
subscribe to my newsletter to get them
Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing flavio@flaviocopes.com. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.