Suggest an editImprove this articleRefine the answer for “What is WeakSet?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)`WeakSet` is a "lightweight" version of `Set` that stores **only objects** and does so via a weak reference (hence *Weak*). **Key point:** it is meant for situations where you want to store unique objects but not stop the garbage collector (GC) from removing them once they are no longer used anywhere else.Shown above the full answer for quick recall.Answer (EN)Image`WeakSet` is a "lightweight" version of `Set`, which stores **only objects** and does so via a **weak reference** (hence the name *Weak*). It is meant for situations where you want to store **unique objects** but **not stop the garbage collector (GC)** from removing them once they are no longer used anywhere else. ## Creating a WeakSet ```javascript const ws = new WeakSet(); const obj1 = { name: "Alice" }; const obj2 = { name: "Bob" }; ws.add(obj1); ws.add(obj2); console.log(ws.has(obj1)); // true ``` - Stores **only objects**, not primitives. (Try adding a string or a number and you get an error.) ```javascript ws.add(123); // TypeError ``` - If `obj1` is no longer used anywhere else, the garbage collector **removes it automatically** from the WeakSet. ## WeakSet methods | Method | Description | Example | |---|---|---| | `add(value)` | adds an object | `ws.add(obj)` | | `delete(value)` | removes an object | `ws.delete(obj)` | | `has(value)` | checks presence | `ws.has(obj)` | **No** `size`, `forEach`, `keys`, `values`, `entries` methods - because `WeakSet` **cannot be iterated**. ## Main difference from Set: | Property | `Set` | `WeakSet` | |---|---|---| | Can store primitives | Yes | No (objects only) | | Elements are "weakly held" (GC can remove them) | No | Yes | | Can be iterated (`for...of`, `size`) | Yes | No | | Used for temporary references | Rarely | Often | ## Example: automatic removal ```javascript let obj = { id: 1 }; const ws = new WeakSet(); ws.add(obj); console.log(ws.has(obj)); // true obj = null; // now the object is no longer reachable // the object will automatically be removed from memory by the garbage collector ``` - After `obj = null` the object becomes **reachable only from the WeakSet**, so the GC (garbage collector) can remove it. - This makes `WeakSet` convenient for **storing temporary or cached objects**. ## Real usage example ### Tracking which objects have already been "processed": ```javascript const processed = new WeakSet(); function process(obj) { if (processed.has(obj)) { console.log("Already processed"); return; } processed.add(obj); console.log("Processing:", obj.name); } const user = { name: "Alice" }; process(user); // Processing: Alice process(user); // Already processed // After user = null the object is automatically removed from the WeakSet ``` ## In short: | Property | Value | |---|---| | Type | A collection of unique **objects** | | Primitives | Not allowed | | Garbage collector | Can remove elements | | Iteration | Not possible | | Methods | `add`, `delete`, `has` | | Common use | Caching, "processed" markers, private data | > **Summary:** > `WeakSet` is a collection of **unique objects** > that are **automatically removed from memory** > once there are no other references to them. It is great for temporary markers and caching, it does not fit if you need to store data permanently or iterate over it.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.