JavaScript Sets
A Set is a collection of unique values. Adding a value that's already in the set does nothing. Sets are ideal for removing duplicates, tracking "have I seen this already?", and fast membership checks.
Creating a Set
Pass any iterable to new Set() — duplicates are dropped automatically, and insertion order is kept:
const tags = new Set(["js", "css", "js", "html", "css"]);
console.log(tags);
console.log(tags.size);
const letters = new Set("banana");
console.log(letters);
const empty = new Set();
console.log(empty);Set(3) { 'js', 'css', 'html' }
3
Set(3) { 'b', 'a', 'n' }
Set(0) {}
Adding, checking and removing
| Method | Does | Returns |
|---|---|---|
add(value) |
adds a value (if new) | the set (chainable) |
has(value) |
checks membership | true / false |
delete(value) |
removes a value | true if it was there |
clear() |
removes everything | undefined |
size |
number of values (a property, not a method) | number |
const visited = new Set();
visited.add("home").add("about").add("home");
console.log(visited.size);
console.log(visited.has("about"), visited.has("blog"));
console.log(visited.delete("about"), visited.delete("blog"));
console.log(visited);2
true false
true false
Set(1) { 'home' }
Sets compare values with SameValueZero — like ===, except that NaN equals NaN. Objects are compared by reference, so two look-alike objects are both kept:
const s = new Set([1, "1", NaN, NaN, { id: 1 }, { id: 1 }]);
console.log(s.size);5
Removing duplicates from an array
The most popular use of a Set is a one-liner:
const emails = ["a@x.com", "b@x.com", "a@x.com", "c@x.com", "b@x.com"];
const unique = [...new Set(emails)];
console.log(unique);
// count distinct words, ignoring case
const text = "The cat and the hat and THE bat";
const distinct = new Set(text.toLowerCase().split(" "));
console.log(distinct.size, [...distinct].join(", "));[ 'a@x.com', 'b@x.com', 'c@x.com' ]
5 the, cat, and, hat, bat
Looping over a Set
Sets are iterable in insertion order. Use for…of, forEach, or spread into an array to use array methods like map and filter:
const primes = new Set([2, 3, 5, 7]);
for (const p of primes) {
console.log(p, p * p);
}
const odd = [...primes].filter((p) => p % 2 === 1);
console.log(odd);2 4
3 9
5 25
7 49
[ 3, 5, 7 ]
Set operations
Combining sets — union, intersection, difference — is easy with spread and filter. (Newer engines also have built-in methods such as a.union(b) and a.intersection(b).)
const frontend = new Set(["ana", "ben", "cara"]);
const backend = new Set(["ben", "dan", "cara"]);
const union = new Set([...frontend, ...backend]);
const both = [...frontend].filter((x) => backend.has(x));
const onlyFront = [...frontend].filter((x) => !backend.has(x));
console.log([...union]);
console.log(both);
console.log(onlyFront);[ 'ana', 'ben', 'cara', 'dan' ]
[ 'ben', 'cara' ]
[ 'ana' ]
Set vs Array
| Set | Array | |
|---|---|---|
| Duplicates | never | allowed |
| Order | insertion order | index order |
| Access by index | ❌ | ✅ arr[2] |
| Membership check | has() — fast, constant time |
includes() — scans every element |
| Methods | add/has/delete | dozens (map, filter, sort…) |
Tip: If you call
array.includes(x)many times inside a loop on a large array, build aSetonce and useset.has(x)instead — it's much faster.
WeakSet is a variant that only holds objects and doesn't prevent them from being garbage-collected; it's used for tagging objects (for example "already processed") without memory leaks.
Test yourself
What is new Set([1, 1, 2, 2, 3]).size?
- 5
- 3
- 2
- 1
How do you turn a Set back into an array?
- set.toArray()
- [...set]
- Array(set)
- set.values
Which method checks whether a Set contains a value?
- includes()
- contains()
- has()
- indexOf()