Example 1 — Add Objects and Chain the Calls
Add an object, check what add() returns, then chain two more calls in a single line.
const marked = new WeakSet();
const first = { id: 1 };
const second = { id: 2 };
const list = [10, 20];
console.log(marked.add(first) === marked); // true
marked.add(second).add(list);
console.log(marked.has(first)); // true
console.log(marked.has(second)); // true
console.log(marked.has(list)); // true How It Works
- A set from add(). The first line prints
truebecausemarked.add(first)hands backmarkeditself, not a new set. - One line, two objects. Since each call returns the set,
.add(list)can follow.add(second)straight away. - Arrays count too. An array is an object in JavaScript, so
listis accepted. All three lookups withhas()printtrue.

