What are JavaScript Sets

Опубликовано: 24 Февраль 2026
на канале: Arvind Programming
107
like

What are JavaScript Sets @arvindprogramming
JavaScript Sets are a built-in data structure that allows you to store unique values of any type, whether they are primitive values or object references. The values stored in a Set can be inserted, deleted, and accessed in constant time. Sets are similar to arrays, but they do not have an index-based structure and do not allow any duplicate values.

Here are some key features of JavaScript Sets:

1. Unique Values: Sets only allow unique values. If you try to add a value that already exists in the Set, it will be ignored.

2. No Order: Sets do not maintain the insertion order of elements. This means that you cannot access elements by their index like in arrays.

3. Iteration: Sets provide various methods for iterating over their elements, such as forEach, for...of loop, and the values() method.

4. Operations: Sets have methods to perform common operations like adding values, deleting values, checking if a value exists, getting the size, and clearing the Set.

Sets can be useful when you need to store a collection of unique values and perform operations based on uniqueness. They are commonly used to eliminate duplicates from an array, implement graph algorithms, and perform set operations like union, intersection, etc.

Example usage of JavaScript Sets:

```
// Creating a Set
const set = new Set();

// Adding values to the Set
set.add(1);
set.add(2);
set.add(3);
set.add(3); // ignored because 3 already exists

// Checking if a value exists in the Set
console.log(set.has(2)); // true

// Getting the size of the Set
console.log(set.size); // 3

// Deleting a value from the Set
set.delete(2);

// Iterating over the Set
set.forEach(value =greater than console.log(value));

// Clearing the Set
set.clear();
```

In summary, JavaScript Sets provide a way to store unique values and perform various operations efficiently.
Social Media Links-
Facebook Page --   / arvindweb   Coaching-Programming & Games/
Facebook -   / arvindweb  
Twitter -   / arvindwebspn  
YouTube -    / @arvindprogramming  
Instagram -   / arvindprogramming