An Introduction to the Set Data Structure in JavaScript

Everything about the set data structure in JavaScript.

Elson TC
Published in
8 min readApr 29, 2021

--

There are many situations where you need to compare multiple lists and extract items they have or are not in common, available in one only, etc. Sets allow you to do just that and more. In particular, the Javascript Set is a very special and powerful one but still lacks important things that other languages offer.

Video Version of This Article

This post is an improved and more detailed article version of the Set Data Structure Series on Youtube that you can check if you prefer videos.

Watch Videos

What is a Set?

Set is a keyed collection of unique items stored in no particular order. Unlike other collection types like Stack, Queue, and Array, a Set is used in list comparison and to test whether an item exists in a set or not. It is correct to say that a Set stores key-key value pairs because you use the item to test if it is in the set.

Set is also an abstract data type which means that it is defined by its behavior much like Stack and Queue Data Structures. Because of its key-key nature, a Set is much more closely related to a Map than anything else and you can even implement a Set using it.

--

--

Elson TC

Software Engineer sharing knowledge, experience, and perspective from an employee and personal point of view.