Example 1 — Remove an Object and Read the Result
Add a session object, remove it, and try to remove it a second time.
const active = new WeakSet();
const session = { user: "Ada" };
active.add(session);
console.log(active.delete(session)); // true
console.log(active.delete(session)); // false
console.log(active.has(session)); // false
console.log(session.user); // Ada How It Works
- First delete wins. The session is in the set, so the first
delete()removes it and answerstrue. - Second delete finds nothing. The set no longer holds it, so the next call answers
false. No error, no warning. - The object lives on.
has()now saysfalse, yetsession.userstill prints "Ada". Deleting only changes the set, never the object.

