JavaScript Sets

A Set is an object in JavaScript that allows you to store unique values, whether they are primitive values or object references. The values in a set can be of any data type, and they are stored in the order they were inserted. A set cannot have duplicate values.

Creating a Set

To create a set in JavaScript, you can use the Set constructor. You can initialize a set with an iterable object such as an array, or you can create an empty set.

// Creating an empty set
const mySet = new Set();

// Creating a set from an array
const myArray = [1, 2, 3];
const mySet = new Set(myArray);

JavaScript Sets Methods

JavaScript Sets has a bult-in methods you can use such as: add(), has(), delete(), size, clear()

add(): Adding Values to a Set

To add a value to a set, you can use the add() method.

const mySet = new Set();
mySet.add("hello");
mySet.add("world");

has(): Checking if a Value is in a Set

To check if a value is in a set, you can use the has() method.

const mySet = new Set(["hello", "world"]);
mySet.has("hello"); // true
mySet.has("goodbye"); // false

delete(): Removing a Value from a Set

To remove a value from a set, you can use the delete() method.

const mySet = new Set(["hello", "world"]);
mySet.delete("hello");

size: Getting the Size of a Set

You can get the size of a set using the size property.

const mySet = new Set(["hello", "world"]);
mySet.size; // 2

clear(): Clearing a Set

To remove all values from a set, you can use the clear() method.

const mySet = new Set(["hello", "world"]);
mySet.clear();

Iterating Over a Set

You can use a for...of loop to iterate over a set.

const mySet = new Set(["hello", "world"]);
for (const value of mySet) {
  console.log(value);
}

You can also use the forEach() method to iterate over a set.

const mySet = new Set(["hello", "world"]);
mySet.forEach((value) => {
  console.log(value);
});

Converting a Set to an Array

You can convert a set to an array using the Array.from() method.

const mySet = new Set(["hello", "world"]);
const myArray = Array.from(mySet);

Using Sets for Union, Intersection, and Difference Operations

Sets can be useful for performing set operations such as union, intersection, and difference.

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

// Union
const union = new Set([...setA, ...setB]); // Set { 1, 2, 3, 4 }

// Intersection
const intersection = new Set([...setA].filter((x) => setB.has(x))); // Set { 2, 3 }

// Difference
const difference = new Set([...setA].filter((x) => !setB.has(x))); // Set { 1 }

JavaScript sets are a powerful tool for storing and manipulating unique values. They can be used for a wide range of applications, from simple data storage to complex set operations. With the various methods available for working with sets, you can easily add, remove, and iterate over values, as well as perform set operations

This page was updated on -