1. What is a set?

A set is a data structure that lets you store unique values in it. A set always contains unique values. A set can hold primitive as well as references to the user-defined objects. A set is a collection of distinct values, you can iterate through the values available in the set one by one.

2. Creating a set object

A set object can be created using the Set() constructor.

var mySet = new Set();

3. Adding values to a set

add() method is used to add values to a set object in javascript.

var mySet = new Set();
mySet.add("Sunday");
mySet.add("Monday");
mySet.add("Tuesday");
mySet.add("Wednesday");
mySet.add("Thursday");
mySet.add("Friday");
mySet.add("Saturday");

4. Iterating the values of a set

There are the following methods to iterate over a set in javascript.

4.1. Using the keys() method

It returns the values in a set in the insertion order.

console.log(mySet.keys());

Output:

SetIterator {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}

4.2. Using the values() method

It is the same as the keys() method. It also returns the values in a set in the insertion order.

console.log(mySet.values());

Output:

SetIterator {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}

4.3. Using the entries() method

entries() returns an array for each element in the set in the form [value, value]. The second value is used as a key the set to keep the values unique in a set.

console.log(mySet.entries());

Output:

SetIterator {"Sunday" => "Sunday", "Monday" => "Monday",, "Tuesday" => "Tuesday", "Wednesday" => "Wednesday", "Thursday" => "Thursday", "Friday" => "Friday", "Saturday" => "Saturday"}

4.4. Using forEach() method

forEach() method can also be used to iterate over the set objects.

for(const day of mySet){
  console.log(day)
}

Output:

Sunday
Monday 
Tuesday 
Wednesday 
Thursday
Friday
Saturday  

4.5. Using the next() method

next() method of "iterator" object can be used to iterate the elements manually. To use this method an iterator object is required. On the iterator object next().value is used to extract its value.

var itr = mySet.keys();
console.log(itr.next().value);
console.log(itr.next().value);

Output:

Sunday
Monday

Note: entries() and value() can also be used to get the iterator object.

5. Removing elements from a set

Elements can be removed from a set in the following ways:

5.1. Deleting elements one by one

delete() method is used to delete the specified value from the set.

mySet.delete("Sunday");
mySet.delete("Saturday");
console.log(mySet.keys());

Output:

SetIterator {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}

5.2. Deleting all the elements 

clear() method is used to delete all the elements of a set.

mySet.clear();
console.log(mySet.size);

Output:

0

6. What is a WeakSet?

WeakSets are just like sets. The only difference is that a "WeakSet" can only hold the objects whereas a "Set" can hold values of any datatype. WeakSets are weak, which means that if there is no other reference to the objects which are available in a "WeakSet", then those objects are garbage collected.

var weakSet = new WeakSet();
var obj = {};
weakSet.add(obj);
console.log(weakSet.has(obj));

Output:

true

Note: If try to add a value other than an object to a "WeakSet" it will give an error.

7. Conclusion

In this article, we discussed the "Sets" in javascript and different methods that are available to work with the sets and we also discussed the "WeakSets" in javascript.