JavaScript Set

A Set in JavaScript is a collection of unique values, where each value must be distinct. Unlike arrays, Sets do not allow duplicate entries, making them ideal for scenarios where uniqueness is crucial. You can initialize a Set with values during creation or add and remove values dynamically.

Creating a Set:
Creating a Set is straightforward, and it can be initiated in two ways:

// Method 1: Using the Set constructor
let mySet = new Set([1, 2, 3, 4, 5]);

// Method 2: Using the add method
let anotherSet = new Set();
anotherSet.add('apple');
anotherSet.add('orange');
anotherSet.add('banana');

In the below PDF we discuss about Javascript Set in detail in simple language, Hope this will help in better understanding.

JavaScript

Accessing Set Elements:

You can access Set elements using the values() method and check if there is an element inside Set using has() method. For example,

const set1 = new Set([1, 2, 3]);

// access the elements of a Set
console.log(set1.values()); // Set Iterator [1, 2, 3]

Adding New Elements:

You can add elements to a Set using the add() method. For example,

const set = new Set([1, 2]);
console.log(set.values());

// adding new elements
set.add(3);
console.log(set.values());

// adding duplicate elements
// does not add to Set
set.add(1);
console.log(set.values());

Output:

Set Iterator [1, 2]
Set Iterator [1, 2, 3]
Set Iterator [1, 2, 3]

Iterate Sets:

You can iterate through the Set elements using the for…of loop or forEach() method. The elements are accessed in the insertion order. For example,

const set = new Set([1, 2, 3]);

// looping through Set
for (let i of set) {
console.log(i);
}

Output:

1
2
3

Key Features of JavaScript Set:

  • Uniqueness: Sets ensure that each element is unique, eliminating the need for manual duplicate checks.
  • No Indexing: Unlike arrays, Sets do not provide indexing or positioning of elements. This design choice reflects their emphasis on uniqueness and unordered nature.
  • Iterable: Sets are iterable, allowing developers to use loops and iterators to traverse through the elements.
  • Efficient Operations: Sets offer efficient methods for adding, deleting, and checking the presence of elements, with constant time complexity.

Related Question

A JavaScript Set is a built-in object in JavaScript that allows you to store unique values of any data type. It provides methods for adding, deleting, and checking the existence of elements in a collection.

You can create a new Set using the Set constructor.

Sets only allow unique values, meaning each element must be distinct. Arrays, on the other hand, can contain duplicate values. Sets are also not indexed like arrays, and they do not have a built-in order.

You can use the add method to add elements to a Set

You can check if a key exists in a Map using the has method. It returns a boolean indicating whether the specified key is present in the Map or not.

 

Relevant

JavaScript JSON JSON stands for

JavaScript Function Generator Function generators,

JavaScript WeakSet A WeakSet is

JavaScript WeakMap A WeakMap is

JavaScript Proxy A JavaScript Proxy

JavaScript Boolean JavaScript Boolean is

JavaScript BigInt JavaScript BigInt represented

Leave a Comment

Your email address will not be published. Required fields are marked *

// Sticky ads