Set
The Set global object stores a collection of unique values of any type and remembers the order in which each value was first added. Adding a value that’s already present changes nothing, so building a set from an array drops the duplicates:
> new Set(["js", "css", "js", "html", "css"])
Set(3) { 'js', 'css', 'html' }
Values are matched with the SameValueZero algorithm. It works like ===, except that NaN matches NaN. Objects match only themselves, so two objects with the same contents are two different values.
Syntax
The Set constructor takes an optional iterable of values:
new Set([<iterable>])
| Parameter | Description |
|---|---|
iterable |
An optional iterable, such as an array, a string, or another Set, whose elements become the set’s values. Repeated elements are added once |
The constructor returns a new Set. The new keyword is required, and calling Set() without it throws a TypeError.
Key Instance Methods and Properties
| Member | Kind | Description |
|---|---|---|
.add() |
Instance method | Adds a value if it isn’t present and returns the set, so calls can be chained |
.has() |
Instance method | Returns whether a value is in the set |
.delete(), .clear() |
Instance method | Remove one value and return whether it existed, or remove every value |
.size |
Instance property | Returns the number of values |
.values(), .keys(), .entries() |
Instance method | Return iterators over the values in insertion order. .keys() is the same function as .values(), and .entries() yields [value, value] pairs |
.forEach() |
Instance method | Calls a function once per value, in insertion order |
.union(), .intersection() |
Instance method | Return a new set with the values in either set, or in both sets (ES2025) |
.difference(), .symmetricDifference() |
Instance method | Return a new set with the values only in this set, or in exactly one of the two sets (ES2025) |
.isSubsetOf(), .isSupersetOf(), .isDisjointFrom() |
Instance method | Return whether every value is in the other set, whether the other set’s values are all in this one, or whether the two share no values (ES2025) |
A Set is iterable, so for...of and spread syntax walk its values in insertion order.
The seven set composition methods were added in ES2025. They’re Baseline Newly available since June 2024 and supported in Node.js 22 and later. The rest of Set is Baseline Widely available.
Examples
A link checker must skip pages it has already visited. .has() answers that question, and .add() records each new page:
> const visited = new Set();
undefined
> const links = ["/", "/about", "/", "/blog", "/about"];
undefined
> for (const link of links) {
… if (!visited.has(link)) {
… visited.add(link);
… }
… }
undefined
> visited
Set(3) { '/', '/about', '/blog' }
A shared document lists who can edit it and who has opened it. The composition methods compare the two groups without a loop:
> const editors = new Set(["ada", "grace", "linus"]);
undefined
> const viewers = new Set(["grace", "linus", "margaret"]);
undefined
> editors.intersection(viewers)
Set(2) { 'grace', 'linus' }
> editors.difference(viewers)
Set(1) { 'ada' }
> new Set(["grace"]).isSubsetOf(editors)
true
Pick a method, or swap which set calls it, to see which members of editors and viewers end up in the returned set:
Common Gotchas
Objects are compared by reference, so two objects with equal contents are both kept:
> new Set([{ id: 1 }, { id: 1 }]).size
2
The composition methods need a set-like argument with .size, .has(), and .keys(). An array has no .size or .has(), so passing one throws a TypeError. Wrapping it in new Set() first works:
> const tags = new Set(["js", "css"]);
undefined
> tags.union(["html"])
Uncaught TypeError: The .size property is NaN
at Set.union (<anonymous>)
> tags.union(new Set(["html"]))
Set(3) { 'js', 'css', 'html' }
Related Resources
Course
JavaScript Map and Set for Pythonistas
Port a community garden's season report to JavaScript, and learn when to use a Map, a Set, or a plain object, and how to count, group, and compare without Counter or defaultdict.
For additional information on related topics, take a look at the following resources:
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Nov. 19, 2026