Set
A Set is a collection of unique values. Adding a value that is already present does nothing, and membership tests are SameValueZero.
Detailed Explanation
A set answers "have I seen this?" in average constant time, and it stores each value once. new Set() is empty. new Set(iterable) adds each item from the iterable, dropping duplicates as it goes. new Set([1, 1, 2]) has size 2.
add, has, delete, and clear are the mutating API. add returns the set. has is the membership test. There is no get; the value is the key. size is the count.
Equality is SameValueZero: === except NaN equals NaN. Objects are compared by reference. Two { id: 1 } objects are two members. To unique-by-id, map to ids first or use a Map from id to object.
Iteration is insertion order of the first time each value was added. A later add of a duplicate does not move it to the end. set.keys(), set.values(), and set.entries() exist for parity with Map; keys and values are the same sequence. for...of yields values.
Sets gained set-algebra methods in ES2025 / recent engines: union, intersection, difference, symmetricDifference, isSubsetOf, isSupersetOf, isDisjointFrom. If you must support older engines, implement those with loops. Spreading [...set] produces an array of unique values, which is the usual "dedupe an array" one-liner: [...new Set(arr)]. That only works for primitive uniqueness or reference uniqueness.
What it costs you: no random access by index (except by turning it into an array), and no deep uniqueness. For small arrays, array.includes in a loop can be clearer and is fine up to tens of items. For thousands of membership tests, a set is the difference between O(n) and O(1).
Reach for a Set when you filter duplicates, record visited nodes in a graph walk, or test membership in a growing collection. Reach for an array when order of duplicates matters or you need indexes. Reach for WeakSet when the members are objects you must not pin.
Code Examples
const seen = new Set();
seen.add("ada@example.com");
seen.add("ada@example.com");
console.log(seen.size); // => 1
console.log(seen.has("ada@example.com")); // => true
const emails = ["a@x.com", "b@x.com", "a@x.com"];
console.log([...new Set(emails)]); // => [ 'a@x.com', 'b@x.com' ]const tags = new Set(["js", "node"]);
tags.delete("js");
console.log([...tags]); // => [ 'node' ]
const nan = new Set([NaN, NaN]);
console.log(nan.size); // => 1
console.log(nan.has(NaN)); // => trueconst a = new Set([1, 2, 3]);
const b = new Set([3, 4]);
if (typeof a.union === "function") {
console.log([...a.union(b)]); // => [ 1, 2, 3, 4 ]
} else {
console.log([...new Set([...a, ...b])]);
}Common Gotchas
Objects are unique by reference
`{ id: 1 }` added twice as two literals yields two members. Unique by field means extracting that field.
`[...new Set(arr)]` does not deep-dedupe
nested objects and arrays compare as references.
Adding a duplicate does not reorder
insertion order is the first insert. Delete and re-add if you want it at the end.
There is no `set[0]`
convert with `[...set]` or iterate. Indexing a set is `undefined`.