In JavaScript, a WeakSet is a collection of objects. Each object can be in it only once. You create one with new WeakSet() and then use just three methods: add(), delete(), and has().
The word weak means the set does not keep its objects alive. When the rest of your program stops using an object, the JavaScript engine is free to clean it up, and the WeakSet forgets it. Because that can happen at any moment, a WeakSet has no size and cannot be looped over.
You use a WeakSet to mark objects without changing them: "already visited", "already processed", "created by my factory". This page teaches the ideas; the WeakSet Methods index lists every method guide.
A WeakSet is a quiet checklist.
You can tick an object, untick it, and ask if it is ticked. You cannot ask how many are ticked, or list them.
Syntax
JavaScript Syntax
Common ways to create a WeakSet and use it:
JavaScript
const seen = new WeakSet(); // empty set
const user = { name: "Ada" };
seen.add(user); // add an object
const known = seen.has(user); // true
seen.delete(user); // true, removed
const both = new WeakSet([user, {}]); // fill it from an array
Reference
Core Ideas
Ideas every WeakSet tutorial builds on:
Idea
Example
Remember
Create
new WeakSet()
Starts empty; an array (or other iterable) of objects is optional
Add
weakSet.add(obj)
Objects only; returns the set
Check
weakSet.has(obj)
true or false, never throws
Remove
weakSet.delete(obj)
true if it was there
Same object
set.has(sameVariable)
Compared by reference, not by contents
No size
typeof set.size
It is "undefined"; you cannot count
No loop
typeof set.forEach
It is "undefined"; you cannot list
Idea
What "Weak" Means
A normal collection, like an array or a Set, keeps every item reachable. As long as the collection exists, so do its items. A WeakSet does not. If nothing else in your program points to an object, being in a WeakSet is not enough to keep it in memory.
When the garbage collector (the engine's memory cleaner) removes such an object, it is simply gone from the set. You cannot watch this happen and you cannot predict when it does. So never write code that waits for an object to disappear. Use the three methods to say what you know right now.
The same idea is behind WeakMap (objects with extra data attached) and WeakRef (a single weak pointer). They are separate tools; this tutorial covers the WeakSet.
Compare
WeakSet vs Set
Question
Set
WeakSet
What can it hold?
Any value
Objects only (and symbols made with Symbol(), since ES2023)
Does it keep objects alive?
Yes, while the set exists
No, it holds them weakly
Can I get its size?
Yes, size
No
Can I loop over it?
Yes
No
Can I clear it?
Yes, clear()
No, make a new WeakSet
Which methods?
add, delete, has, and more
add, delete, has only
Use cases
When to Use a WeakSet
A WeakSet fits when you only need to answer "have I seen this object before?":
Job
How the WeakSet helps
Visited objects
Mark each object you walk through, so loops between objects stop.
Already processed
Skip an object you have handled, without adding a flag to it.
Brand check
Remember objects made by your own factory and accept only those.
DOM elements
Note which elements already have a listener, without keeping removed elements in memory.
Code
JavaScript Examples
Three short programs that show how a WeakSet behaves.
Example 1 — Handle Each Object Only Once
A WeakSet remembers which orders were already handled, so the same order is never processed twice.
A WeakSet is a collection of objects with no duplicates. You add objects, remove them, and ask whether one is inside. It holds them weakly, which means it does not stop the engine from cleaning up an object that nothing else uses.
A Set can hold any value, has a size, and can be looped over. A WeakSet holds only objects, has no size, and cannot be looped over, because its contents may disappear when objects are cleaned up.
Objects in a WeakSet can be removed by the engine at any time you cannot see. If you could count or list them, results would change unpredictably. So only add(), delete(), and has() exist.
Use it to mark objects without changing them: visited nodes in a graph, objects already processed, items already shown, or a simple private brand check. If the objects are not needed, the marks do not keep them in memory.
No. add() throws a TypeError for numbers, strings, booleans, null, and undefined. Use a regular Set for those. Since ES2023, a symbol made with Symbol() is also allowed as a value in recent engines.