Set
Set is a built-in object that stores unique values of any type, whether primitive values or object references. It provides O(1) time complexity for adding, removing, and checking for the existence of elements, making it efficient for deduplication and membership testing.
Syntax
new Set()
new Set(iterable)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
iterable | Iterable | undefined | An iterable object whose elements will be added to the new Set |
Examples
Creating and populating a Set
const fruits = new Set();
fruits.add('apple');
fruits.add('banana');
fruits.add('orange');
// Adding a duplicate - ignored
fruits.add('apple');
console.log(fruits.size);
// 3
console.log(fruits.has('banana'));
// true
Creating a Set from an array
const nums = [1, 2, 3, 2, 1, 4, 5, 3];
const uniqueNums = new Set(nums);
console.log([...uniqueNums]);
// [1, 2, 3, 4, 5]
console.log(uniqueNums.size);
// 5
Iterating over a Set
const colors = new Set(['red', 'green', 'blue']);
// Using for...of
for (const color of colors) {
console.log(color);
}
// red
// green
// blue
// Using forEach
colors.forEach(value => {
console.log(value);
});
Removing elements
const nums = new Set([1, 2, 3, 4, 5]);
nums.delete(3);
console.log(nums.has(3));
// false
nums.clear();
console.log(nums.size);
// 0
Common Patterns
Deduplicating an array
const duplicateNames = ['Alice', 'Bob', 'Alice', 'Charlie', 'Bob'];
const uniqueNames = [...new Set(duplicateNames)];
// ['Alice', 'Bob', 'Charlie']
Case-insensitive string uniqueness
const strings = ['Hello', 'hello', 'HELLO', 'World'];
const unique = new Set(strings.map(s => s.toLowerCase()));
// Set { 'hello', 'world' }
Finding intersection of two arrays
const a = [1, 2, 3, 4];
const b = [3, 4, 5, 6];
const setA = new Set(a);
const intersection = b.filter(x => setA.has(x));
// [3, 4]
Removing duplicates from a string
const str = 'hello world';
const uniqueChars = [...new Set(str)].join('');
// 'helo wrd'
Set operations: union, intersection, difference
const setA = new Set([1, 2, 3, 4]);
const setB = new Set([3, 4, 5, 6]);
// Union
const union = new Set([...setA, ...setB]);
// Set { 1, 2, 3, 4, 5, 6 }
// Intersection
const intersection = [...setA].filter(x => setB.has(x));
// [3, 4]
// Difference (A - B)
const difference = [...setA].filter(x => !setB.has(x));
// [1, 2]
Performance Considerations
The Set object provides constant-time O(1) performance for its core operations: add(), delete(), has(), and size. This makes it significantly faster than using Array methods like includes() or indexOf() for large collections, where arrays would have O(n) linear search time.
When working with large datasets that require frequent membership checks or deduplication, prefer Set over Array. The trade-off is that Sets do not support random access by index—you must iterate through them to retrieve values.
Constructor details
The Set constructor accepts an optional iterable argument. If provided, all elements from the iterable will be added to the Set in order. This works with arrays, strings, other Sets, NodeLists, and any object implementing the iterable protocol.
// From a string - iterates over characters
const chars = new Set('hello');
// Set { 'h', 'e', 'l', 'o' }
// From a Map - iterates over [key, value] pairs
const map = new Map([['a', 1], ['b', 2]]);
const fromMap = new Set(map.keys());
// Set { 'a', 'b' }