TExcelTAD
Home › JavaScript › JS Sets

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:

js · try it yourself
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
js · try it yourself
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:

js · try it yourself
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:

js · try it yourself
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:

js · try it yourself
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).)

js · try it yourself
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 a Set once and use set.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?

  1. 5
  2. 3
  3. 2
  4. 1

How do you turn a Set back into an array?

  1. set.toArray()
  2. [...set]
  3. Array(set)
  4. set.values

Which method checks whether a Set contains a value?

  1. includes()
  2. contains()
  3. has()
  4. indexOf()