In Javascript, a set is a grouping of specific items. Sets are used to represent objects and their attributes in mathematics. In computer programming, sets are often used to describe data structures where duplicates are prohibited. The various set operations that JavaScript supports will be covered in this article.
What is a Set in JavaScript?
A set in JavaScript is a data structure that represents a collection of unique elements. The elements in a set are called members, and sets are often used to describe mathematical sets. Sets can be used to store and manipulate data and perform set operations such as union, intersection, and difference.
The Set Object in JavaScript
The Set object in JavaScript is a collection of unique elements. The Set object is a built-in object in JavaScript, and it is used to store unique values of any type, including objects and primitive values. The Set object represents sets in JavaScript and provides several methods for manipulating and operating on sets.
Creating a Set in JavaScript
A set can be created in JavaScript using the Set object constructor. The Set constructor takes an iterable object as its argument and returns a new Set object. An iterable object is an object that implements the Iterable interface, which means that it can be looped over using a for…of the loop.
let set = new Set([1, 2, 3, 4, 5]);
Adding Elements to a Set in JavaScript
Elements can be added to a set in JavaScript using the add() method. The add() method takes a value as its argument and adds it to the set. If the value already exists in the set, it is not added again.
let set = new Set();
set.add(1);
set.add(2);
set.add(3);
Removing Elements from a Set in JavaScript
Elements can be removed from a set in JavaScript using the delete() method. The delete() method takes a value as its argument and removes it from the set. If the value does not exist in the set, the delete() method returns false.
let set = new Set([1, 2, 3, 4, 5]);
set.delete(3);
Checking if a Value is in a Set
To check if an element exists in a set in JavaScript, use the has() method. The has() method takes a value as its argument and returns true if the value exists in the set, and false otherwise.
let set = new Set([1, 2, 3, 4, 5]);
console.log(set.has(3)); // true
console.log(set.has(6)); // false
The Size of a Set
You can get the size of a Set in JavaScript by using the size
property. The size
property returns the number of values in the set.
let set = new Set([1, 2, 3, 4, 5]);
console.log(set.size); // 5
In this example, we’ve used the size
property to get the size of the set, which is 5.
Iterating Over a Set
You can iterate over a Set in JavaScript using a for...of
loop or the forEach
method. The for...of
loop allows you to access each value in the set one by one. The forEach
method allows you to perform an action for each value in the set.
let set = new Set([1, 2, 3, 4, 5]);
// Using a for...of loop
for (let value of set) {
console.log(value);
}
// Using the forEach method
set.forEach(value => console.log(value));
In this example, we’ve shown two different ways of iterating over a set in JavaScript. The for...of
loop and the forEach
method both allow us to access each value in the set and perform an action on it.
Union of Sets
The union of two sets is a set that contains all the elements of both sets. In JavaScript, the union of two sets can be calculated using the spread operator (…). To calculate the union of two sets, we can create two sets and then use the spread operator to combine them into a new set.
let setA = new Set([1, 2, 3, 4]);
let setB = new Set([3, 4, 5, 6]);
let union = new Set([...setA, ...setB]);
console.log(union); // Set {1, 2, 3, 4, 5, 6}
Intersection of Sets
The intersection of two sets is a set that contains only the elements that are common to both sets. In JavaScript, the intersection of two sets can be calculated using the filter() method. To calculate the intersection of two sets, we can create two sets and then use the filter() method to keep only the elements in both sets.
let setA = new Set([1, 2, 3, 4]);
let setB = new Set([3, 4, 5, 6]);
let intersection = new Set([...setA].filter(x => setB.has(x)));
console.log(intersection); // Set {3, 4}
Difference of Sets
The difference between two sets is that a set contains elements in one set but not in the other. In JavaScript, the difference between two sets can be calculated using the filter() method. To calculate the difference between two sets, we can create two sets and then use the filter() method to keep only the elements in one set but not the other.
let setA = new Set([1, 2, 3, 4]);
let setB = new Set([3, 4, 5, 6]);
let difference = new Set([...setA].filter(x => !setB.has(x)));
console.log(difference); // Set {1, 2}
Complement of Sets
The complement of a set is the set of all elements that do not appear in the set. In mathematical terms, it is represented as Ā, where A is a set. In JavaScript, the complement of a set can be achieved by using the difference between the set and the universal set.
let setA = new Set([1, 2, 3, 4]);
let universal = new Set([1, 2, 3, 4, 5, 6]);
let complement = new Set([...universal].filter(x => !setA.has(x)));
console.log(complement); // Set {5, 6}
Thanks for reading. Happy coding!