WeakSet
The WeakSet global object holds a collection of unique objects or non-registered symbols, and it doesn’t keep those values alive. Once nothing else in the program references a value, the garbage collector can reclaim it, and it disappears from the set. This makes a WeakSet a way to mark objects, such as “already processed” or “created by this class,” without changing them or extending their lifetime:
> const reviewed = new WeakSet();
undefined
> const draft = { title: "Release notes" };
undefined
> reviewed.add(draft)
WeakSet { <items unknown> }
> reviewed.has(draft)
true
> reviewed.has({ title: "Release notes" })
false
Values match by identity, so an object with the same contents is a different value. Because members can vanish whenever the garbage collector runs, a WeakSet has no .size and no way to list its values. It isn’t iterable, and the only way to read it is to pass a value to .has().
Syntax
The WeakSet constructor takes an optional iterable of values:
new WeakSet([<iterable>])
| Parameter | Description |
|---|---|
iterable |
An optional iterable, such as an array, whose elements are added to the set. Each element must be an object or a non-registered symbol |
The constructor returns a new WeakSet. The new keyword is required, and calling WeakSet() 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() |
Instance method | Removes a value and returns whether it was present |
Symbols became valid values in ES2023, as long as they aren’t registered with Symbol.for(). That support is Baseline Newly available since 2025 and available in Node.js 20 and later. The rest of WeakSet is Baseline Widely available.
Examples
A function that walks a tree of nested objects must not loop forever when two nodes point at each other. A WeakSet records each node on the first visit, and the walk skips any node it has already seen:
> function countNodes(node, seen = new WeakSet()) {
… if (seen.has(node)) {
… return 0;
… }
… seen.add(node);
… let count = 1;
… for (const child of node.children) {
… count += countNodes(child, seen);
… }
… return count;
… }
undefined
> const root = { name: "root", children: [] };
undefined
> const docs = { name: "docs", children: [root] };
undefined
> root.children.push(docs);
1
> countNodes(root)
2
The set holds the visited nodes only weakly, so it never keeps a node alive once the rest of the program lets go of it.
A login module needs to reject session objects that weren’t created by its own class, even when they have the same shape. The constructor adds each new instance to a WeakSet, and a static method checks membership:
> const issued = new WeakSet();
undefined
> class Session {
… constructor(user) {
… this.user = user;
… issued.add(this);
… }
… static isGenuine(value) {
… return issued.has(value);
… }
… }
undefined
> Session.isGenuine(new Session("ada"))
true
> Session.isGenuine({ user: "ada" })
false
A copied or hand-built object never passes the check, and a discarded session doesn’t stay in memory because of it.
Common Gotchas
Primitives such as strings and numbers can’t be held weakly, and neither can symbols registered with Symbol.for(). Adding one throws a TypeError, but .has() and .delete() return false instead of throwing:
> const flagged = new WeakSet();
undefined
> flagged.add("spam")
Uncaught TypeError: Invalid value used in weak set
at WeakSet.add (<anonymous>)
> flagged.has("spam")
false
> flagged.add(Symbol.for("spam"))
Uncaught TypeError: Invalid value used in weak set
at WeakSet.add (<anonymous>)
> flagged.add(Symbol("spam"))
WeakSet { <items unknown> }
A WeakSet can’t be spread, looped over, or measured, so code that needs to list or count its values needs a Set instead:
> const visited = new WeakSet([{ id: 1 }]);
undefined
> visited.size
undefined
> [...visited]
Uncaught TypeError: visited is not iterable
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.
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. 12, 2026