Sets in JavaScript

Modern Data structures in JavaScript

Sets in JavaScript

Sets are a type of collection consisting of unique values. They were introduced in ES6+.

Syntax

Let's first see it in use

const firstNames = new Set(['Mary', 'Raj', 'John', 'Aftab', 'Mary']);

In the above code, we see how to declare and initialize a set using the new keyword like so: new Set(iterable).

Tip to practice along with the article: ctrl + shift + I to open your browser's Developer Tool interface. Now, open a new Snippet under the Sources tab and write the code and console.log() it to test out what you learned. To run the code: ctrl + enter

In the above example, we used an array to add values. There's also another way, i.e. by using the Set.add() method.

firstName.add('John');
firstName.add('Kia');

Now, you would think 'John' would get added twice, but you see Sets do not repeat values, hence though we add 'John' again, the Set will contain only one instance of that name.

The above method can be futile, hence using arrays is the best way to add elements to Sets.

Set methods

Here are some in-built Set methods you can use:

  • Set.has(value); to check if the set had the element
  • Set.add(value); to add an element to the set
  • Set.delete(value); to delete particular element from the set
  • Set.clear(); to delete all elements of the set
  • Set.size; to get the number of elements in set (counts unique values)

Example:

firstName.has('John');  // true
firstName.delete('Mary');  // { "Raj", "John", "Aftab"}
firstName.size;  // 3
firstName.clear();   // {}

Arrays and Sets

Now you may ask if there exist Sets why use Arrays? Here's the thing, Set elements are not indexed/ positioned. So we cannot directly get an element by its position/number. Hence, when we want to manipulate data using the element's position, the best way is to use Arrays (instead of Sets).

So if you were to type the following, it would show undefined.

console.log(firstName[0]);

Arrays are also used when you want an ordered list of values, and where unique values are not a requirement.

So, where should we then use Sets? We can use sets to store unique values, i.e. when we do not want any duplicates in our data. Also, some set operations are faster than some Array operations, so both have their use cases.

Remember Sets are also Iterable.


Here's a practical example:

const studentID = new Set([101, 102, 103, 103, 104, 105]);
console.log(studentID);  // {101, 102, 103, 104, 105}

// following will print the list of Student ID's to the console
for(const value of studentID){
console.log(value);
}

// to create an Array from Sets
const studentDetails = [...studentID];

✔Summary

  • Sets are a collection of unique values.
  • The elements of sets are not indexed, hence we use them in cases where indexing is not necessary.
  • Sets have in-built methods like .has(), .delete(), .size that we can use.
  • Sets are Iterable.
  • Sets are not meant to replace Arrays, both have their use cases.

I hope you find the article helpful! Thank you for reading! Comment down your feedback and suggestions, I am always open to it! 😃

You can always reach out to me at LinkedIn | Twitter 🙋‍♀️