JavaScript Sets

In this chapter, we will learn about JavaScript Sets. A Set is a collection of unique values. Each value can only occur once in a Set. We will cover:

  • What is a Set?
  • Creating Sets
  • Adding and Removing Elements
  • Iterating Over Sets
  • Set Methods
  • Converting Sets to Arrays
  • Simple Programs using Sets

What is a Set?

A Set is a collection of unique values, meaning that a value can only appear once in a Set. Sets are particularly useful for storing collections of values where duplicates are not allowed.

Creating Sets

You can create a Set using the Set constructor. You can also initialize a Set with an array of values.

Syntax

let set = new Set(); let set = new Set(iterable); 

Example

let set1 = new Set(); let set2 = new Set([1, 2, 3, 4, 5]); console.log(set1); // Set {} console.log(set2); // Set { 1, 2, 3, 4, 5 } 

Adding and Removing Elements

You can add and remove elements from a Set using the add() and delete() methods.

Example

let set = new Set(); set.add(1); set.add(2); set.add(3); set.add(2); // Duplicate value, won't be added console.log(set); // Set { 1, 2, 3 } set.delete(2); console.log(set); // Set { 1, 3 } 

Iterating Over Sets

You can iterate over the elements of a Set using various methods such as for...of, forEach(), and others.

Example

let set = new Set([1, 2, 3]); // Using for...of for (let value of set) { console.log(value); } // Using forEach set.forEach(value => { console.log(value); }); 

Output:

1 2 3 1 2 3 

Set Methods

Sets provide several useful methods for working with their elements.

has()

Checks if a value exists in the Set.

let set = new Set([1, 2, 3]); console.log(set.has(2)); // true console.log(set.has(4)); // false 

clear()

Removes all elements from the Set.

let set = new Set([1, 2, 3]); set.clear(); console.log(set); // Set {} 

size

Returns the number of elements in the Set.

let set = new Set([1, 2, 3]); console.log(set.size); // 3 

Converting Sets to Arrays

You can convert a Set to an array using the spread operator or the Array.from() method.

Example

let set = new Set([1, 2, 3]); let array1 = [...set]; let array2 = Array.from(set); console.log(array1); // [ 1, 2, 3 ] console.log(array2); // [ 1, 2, 3 ] 

Simple Programs using Sets

Program 1: Removing Duplicates from an Array

function removeDuplicates(array) { return [...new Set(array)]; } let numbers = [1, 2, 2, 3, 4, 4, 5]; let uniqueNumbers = removeDuplicates(numbers); console.log(uniqueNumbers); 

Output:

[ 1, 2, 3, 4, 5 ] 

Program 2: Checking for Unique Values

function hasUniqueValues(array) { let set = new Set(array); return set.size === array.length; } let numbers1 = [1, 2, 3, 4, 5]; let numbers2 = [1, 2, 2, 3, 4]; console.log(hasUniqueValues(numbers1)); // true console.log(hasUniqueValues(numbers2)); // false 

Output:

true false 

Conclusion

In this chapter, you learned about JavaScript Sets, including how to create Sets, add and remove elements, iterate over Sets, use Set methods, and convert Sets to arrays. We also provided various use cases with simple programs to demonstrate the usage of Sets. Understanding how to effectively use Sets is essential for managing collections of unique values in JavaScript.

Leave a Comment

Scroll to Top