The Set data structure in JavaScript allows you to store and manage a collection of objects or primitive types. It is similar to a Map, where the values are used as keys and the map value is always a boolean true.

Table of Contents

What is a Set?

A Set is a data structure introduced in ECMAScript 6 (ES2015) that allows you to add data to a container. It can store objects or primitive types like strings, numbers, and booleans. You can think of a Set as a Map where values are used as keys and the map value is always a boolean true.

Initialize a Set

To initialize a Set, you can create a new instance of the Set class as follows:

const s = new Set();

Add items to a Set

You can add items to a Set using the add method:

s.add('one');
s.add('two');

A Set only stores unique elements, so adding the same item multiple times won’t create duplicate entries. To add multiple elements to a Set, you need to call the add() method for each element separately.

Check if an item is in the set

To check if an item exists in a Set, you can use the has method:

s.has('one'); // returns true
s.has('three'); // returns false

Delete an item from a Set by key

To delete an item from a Set by its key, you can use the delete method:

s.delete('one');

Determine the number of items in a Set

To get the number of items in a Set, you can access the size property:

s.size;

Delete all items from a Set

To remove all items from a Set, you can use the clear method:

s.clear();

Iterate the items in a Set

You can iterate through the items in a Set using the keys() or values() methods. They are equivalent and return an iterator:

for (const k of s.keys()) {
    console.log(k);
}

for (const k of s.values()) {
    console.log(k);
}

You can also use the entries() method to get an iterator that returns each element as a { value, done = false } object until the iterator ends:

const i = s.entries();
console.log(i.next());

Alternatively, you can use the forEach() method or a for...of loop directly on the Set to iterate over its items.

Initialize a Set with values

You can initialize a Set with a set of values by passing an array of values to the Set constructor:

const s = new Set([1, 2, 3, 4]);

Convert to array

You can convert the keys or values of a Set into an array using the spread operator:

const a = [...s.keys()];

// or

const a = [...s.values()];

A WeakSet

A WeakSet is a special kind of Set in JavaScript. In a WeakSet, the items can be garbage collected when there is no active reference to them. Unlike a regular Set, a WeakSet can only store objects as its keys. WeakSet is often used in framework-level code.

Here are the main differences of WeakSet compared to a regular Set:

  1. WeakSets cannot be iterated over.
  2. WeakSets do not have a method to clear all items at once.
  3. WeakSets do not have a method to check their size.

A WeakSet mainly offers the following methods:

  • add(): Add an item to the WeakSet.
  • has(): Check if an item exists in the WeakSet.
  • delete(): Delete an item from the WeakSet.