Map.prototype.clear()
Removes every entry from the map in one call, leaving it empty but still the same object.
clear()- Added in JavaScript ES2015
Return value
undefinedundefined. The method mutates the map in place and returns nothing.
Errors
Throws a TypeError when called on a value that is not a real Map.
Changelog
- PHP ES2015Introduced with the Map built-in.
clear() empties the map while keeping the identity of the object. That distinction is the whole point of choosing it over map = new Map(): every other reference to the same map — a field on a class, a value captured in a closure, an entry in another collection — sees the map become empty. Reassigning the variable would leave all those references pointing at the old, still-full map.
Typical use
Cache invalidation, resetting per-request state, and teardown between test cases. It is a single operation rather than a loop of delete() calls, so prefer it when you want everything gone.
What it does not do
It returns undefined, not the map, so it cannot be chained. It does not touch the values themselves — objects that were stored remain alive as long as something else references them; clearing only drops the map's own references to them.
An iterator obtained before the clear yields nothing afterwards, because the entries it would have visited are gone. Calling clear() on an already-empty map is harmless.
Example
When to use it
- Invalidate a whole memoization cache after underlying data changes
- Reset shared state between test cases without reassigning the variable
- Empty a registry that other modules already hold a reference to
- Drop all buffered entries when a connection closes
Frequently asked questions
What is the difference between clear() and creating a new Map?
clear() empties the existing object, so every reference to it sees the change; assigning a new Map only rebinds one variable and leaves other holders with the old contents.
Does Map.clear() return the map?
No, it returns undefined. Unlike set(), it cannot be used in a chain.
Does clearing a Map free the memory used by its values?
It drops the map's references to them, so values become eligible for collection only if nothing else still references them.
More examples
clear() versus reassigning the variable
Reassigning gives the current variable a fresh map but leaves every other holder looking at the old contents. clear() empties the one object everybody shares, which is what "reset the cache" almost always means.
let store = new Map([["x", 1]]);
const held = store; // something else kept a reference
store = new Map(); // reassign
console.log(store.size, held.size); // 0 1 — held is stale
store = held;
store.clear(); // mutate instead
console.log(store.size, held.size); // 0 0Clearing invalidates outstanding iterators
The iterator had not yet reached "b" when the map was cleared, so it finishes immediately. A new iterator created after the set() sees the fresh entry normally.
const m = new Map([["a", 1], ["b", 2]]);
const it = m.keys();
console.log(it.next()); // { value: 'a', done: false }
m.clear();
console.log(it.next()); // { value: undefined, done: true }
m.set("c", 3);
console.log([...m.keys()]);
Discussion