Set in JavaScript

Joseph Kniskern
1 min readFeb 4, 2021

A useful tool for solving problems in JavaScript is always a good thing to have. Thanks to ES6 we have the set object.

The set object in JavaScript will create an object of unique values. You then have a few methods available to work with your set. You can add elements to your set with .add, but remember it only stores unique values, so any redundant elements won’t be included. You can delete values with .delete, however, unlike .add you cannot chain them to delete multiple values. You can clear all the elements with .clear, and you can check if an element is in the set with .has.

There are even more ways to work with your set through iteration. You can use .keys or .values to return elements, and of course .forEach.

The most common usage is for finding unique elements in an array, but as you can see there are many ways set can be useful.

--

--