Set.prototype.clear()
Removes every element at once, leaving the same object empty and reusable.
clear()- Added in JavaScript ES2015
Return value
undefinedundefined.
Errors
Throws a TypeError if called on a value that is not a real Set instance. Otherwise it never throws, even on an already-empty collection.
clear() empties the collection in place. It takes no arguments, returns undefined, and is safe to call on something that is already empty. Afterwards size is 0 and the object is fully reusable — you can keep adding to it.
Why in-place matters
The alternative to clearing is reassignment: cache = new Set(). Those are not equivalent. Reassignment gives you a brand-new object and leaves every other holder of the old reference — a closure, an array element, another module's import — pointing at the old, still-populated collection. clear() empties the one object everybody shares. If the collection is a const, or was captured before you got here, clearing is the only option.
The flip side is that clearing is visible everywhere. If some code holds the same reference and expects its contents to persist, you have just emptied its data too. Hand out new Set(original) copies if callers should be insulated.
Iterators
An iterator created before the clear simply finds nothing left and finishes. It does not throw and does not resurrect the old values.
Example
When to use it
- Flush a memoization or dedupe cache between test runs
- Reset the selected-rows collection when a table reloads
- Empty a shared registry held as a const without breaking existing references
- Release the object references a long-lived collection is keeping alive
Frequently asked questions
Does clear() return the emptied Set?
No, it returns undefined, so it cannot be chained the way add() can. Call it as a standalone statement and use the collection itself afterwards.
Should I use clear() or just create a new Set?
Use clear() when other code holds the same reference, or the variable is a const — it empties the shared object. Create a new Set when you want the old contents to stay intact for anyone still holding them.
Is it an error to clear an empty Set?
No. Clearing an already-empty collection is a harmless no-op that still returns undefined, so there is no need to guard the call with a size check.
More examples
Clearing versus reassigning
After reassignment the local variable is empty but holder.ref still sees all three values — the old object was never touched. Calling clear() instead empties the single object both names point at, so both report 0.
let shared = new Set([1, 2, 3]);
const holder = { ref: shared };
shared = new Set();
console.log(shared.size, holder.ref.size);
shared = holder.ref;
shared.clear();
console.log(shared.size, holder.ref.size);An iterator taken before the clear
The iterator yields "x", then reports done after the clear rather than throwing. Values added afterwards are not picked up either — a finished set iterator stays finished.
const s = new Set(["x", "y", "z"]);
const it = s.values();
console.log(it.next().value);
s.clear();
console.log(it.next());
console.log(s.size);
s.add("new");
console.log(it.next());Resetting a dedupe pass between batches
Clearing at the top of each iteration reuses one object instead of allocating a fresh collection per batch, and keeps the value 2 from leaking between batches. The final size reflects only the last batch.
const seen = new Set();
const batches = [[1, 1, 2], [2, 3, 3]];
for (const batch of batches) {
seen.clear();
for (const n of batch) seen.add(n);
console.log("batch unique:", [...seen]);
}
console.log("after loop:", seen.size);
Discussion