JavaScript WeakSet Methods

About This Index
This page is the JavaScript WeakSet Methods index for the tutorial track. Each row links to a full guide with syntax, three runnable examples, browser notes, and FAQs.
Learn what a WeakSet is on /js/weakset. Use this page when you need a specific method such as add(), delete(), or has().
A WeakSet holds objects and nothing else. You can add, remove, and check them, but you cannot count them or loop over them.
How to Call a WeakSet Method
Create the set with new, then use a dot on it:
const set = new WeakSet();
const user = { name: "Ada" };
set.add(user); // add an object, returns the set
set.has(user); // true
set.delete(user); // true (it was there, now removed)
set.has(user); // false JavaScript WeakSet Methods Table
All method guides in this JavaScript tutorial series (3 tutorials), grouped by what they do.
Add objects
1 methodPut an object into the set. A WeakSet only accepts objects, and adding the same object twice changes nothing.
Remove objects
1 methodTake one object out of the set and find out whether it was really there.
Check membership
1 methodAsk whether an exact object is in the set. This is the only way to read a WeakSet.
WeakSet Constructor and Missing Members
These are used with new or read with a dot. The last rows list what a WeakSet does not have, so you do not look for it.
| Member | What it gives you | Since |
|---|---|---|
new WeakSet() | Create an empty WeakSet. | ES2015 |
new WeakSet(iterable) | Create a WeakSet and call add() for every object in the iterable, such as an array. Any item that is not an object throws a TypeError. | ES2015 |
weakSet.size | Does not exist. Reading it gives undefined, because a WeakSet cannot be counted. | Never |
weakSet.clear() | Does not exist. To start over, replace the set with a new WeakSet. | Never |
weakSet.forEach() | Does not exist, and neither do keys(), values(), entries(), or for...of. A WeakSet cannot be looped over. | Never |
weakSet.add(symbol) | A symbol made with Symbol() is accepted as a value. A symbol from Symbol.for() still throws a TypeError. | ES2023 |
Frequently Asked Questions
Start with add()
Learn how to put objects into a WeakSet, and why a number or string throws.
14 people found this page helpful
