In short
A WeakSet is a Set for objects that doesn't hold on to them tightly. Use it to mark objects as "seen" or "done" without causing memory leaks.
Like a Set, but can only hold objects (not primitives) and holds them weakly, allowing garbage collection once nothing else references them. Its weak references make it suitable for tracking object membership without keeping those objects alive solely because they are present in the collection.

Think of it like this
Ticks on the attendance sheet
The teacher ticks names on today's sheet. If a student leaves college for good, their tick quietly disappears with them. You can ask "is this student ticked?", but you can't print the full list of ticks.
Watch it happen
Sending the welcome email only once
has(user)
1/7First call. Is this exact user object in the WeakSet?
The big ideas
What you need to know
Objects only
Like WeakMap, a WeakSet can only hold objects. Adding a number or string throws a TypeError.
Just add, has, delete
No size, no looping. You can only ask about objects you already have in hand.
Doesn't keep things alive
If the only reference to an object is inside the WeakSet, the object can still be garbage collected.
Great for marking
Track which objects you've already processed, or which nodes you've visited while walking nested data.
See it in code
Walk through the code
1const processed = new WeakSet();2 3function sendWelcomeEmail(user) {4 if (processed.has(user)) return "Already sent";5 processed.add(user);6 return "Email sent to " + user.email;7}8 9const user = { email: "tanya@example.com" };10console.log(sendWelcomeEmail(user)); // "Email sent to tanya@example.com"11console.log(sendWelcomeEmail(user)); // "Already sent"12 13// When 'user' isn't referenced anywhere else, it can be collected.14// The WeakSet won't keep it alive.1/4A WeakSet to remember which users are done.
You get a cheap "done" flag without adding an extra property to the user object, and without leaking memory.
Save yourself the pain
Common mistakes
Oops
Expecting weakSet.size or a for...of loop to work.
Do this instead
Neither exists on a WeakSet. If you need to count or list items, use a normal Set.
Oops
Checking with a new object that looks the same, like has({ id: 1 }).
Do this instead
WeakSet compares by identity. You must pass the exact same object you added.
Your mission
Stop double clicks
Quick check
What does this log? const ws = new WeakSet(); ws.add({ id: 1 }); console.log(ws.has({ id: 1 }));
Go deeper
- 1Read a use case for WeakSet: tracking which objects have been 'processed' without leaking. Then apply the idea in a small example so the theory becomes practical.
- 2Compare WeakSet to Set — note WeakSet also isn't iterable. Write down when you would choose each option and what trade-off drives that choice.
- 3Understand this is a niche tool — most day-to-day code uses Set, not WeakSet.
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.