JavaScript Map clear() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Map

Definition

map.clear() removes every entry from a Map, so its size becomes 0. The Map object itself is not replaced: it is the same object as before, only empty. Every variable that points to the Map sees the change. The method returns undefined.

Remember
map.clear()  → undefined

before  → map.size is 3 (for example)
after   → map.size is 0
returns → undefined

Same Map object, now empty
Other variables that point to it see the change

Syntax

Call it on a Map. It takes no arguments.

JavaScript
map.clear()
CallWhat it does
map.clear()Remove all entries. The Map stays usable and you can set new keys later.
map = new Map()Replace the variable with a brand-new empty Map. Other variables that point to the old Map still see the old entries.

Built into JavaScript since ES2015. To remove just one key, see delete().

Library declarations
JavaScript
map.clear()   // returns undefined

// no parameters
// empties the same Map object

Parameters

ParameterDescription
(none)This method takes no parameters. Call it as map.clear().

Return Value

Return ValueDescription
undefinedNothing useful is returned. Read map.size to see that it is now 0.

JavaScript Examples

Example 1 — Empty a Map

Create a Map with three tags, empty it, and look at the size, the return value, and a lookup.

JavaScript
const tags = new Map([
  ["js", true],
  ["css", true],
  ["html", true]
]);
console.log(tags.size);                 // 3

const result = tags.clear();
console.log(tags.size);                 // 0
console.log(String(result));            // undefined
console.log(tags.has("js"));            // false
Try It Yourself

How It Works

  1. Start with values. The Map is created from three pairs, so its size is 3.
  2. Clear it. tags.clear() removes everything. It returns undefined, so result prints as undefined.
  3. Nothing is left. The size is 0 and has("js") is false, because the old entries are gone.

Example 2 — Same Object, Shared References

Two variables point to one Map. Clear it through one variable and add a new entry, then read it through the other one.

JavaScript
const scores = new Map([["Ann", 10], ["Bob", 8]]);
const sameScores = scores;

scores.clear();
scores.set("Carl", 5);

console.log(sameScores.size);                 // 1
console.log(sameScores.get("Carl"));          // 5
console.log(scores === sameScores);           // true
Try It Yourself

How It Works

  1. Two names, one Map. sameScores is not a copy. Both variables hold the same Map object.
  2. Clear and refill. clear() removes Ann and Bob, and the Map is still the same object, so set("Carl", 5) works on it right away.
  3. Both variables agree. Reading through sameScores shows Carl, and === confirms that it is still the very same Map.

Example 3 — clear() vs new Map()

Compare emptying the same Map with clear() against pointing one variable at a brand-new Map.

JavaScript
const a = new Map([["x", 1]]);
const b = a;
a.clear();
console.log(b.size);          // 0

let c = new Map([["y", 2]]);
const d = c;
c = new Map();
console.log(d.size);          // 1
console.log(c.size);          // 0
Try It Yourself

How It Works

  1. clear() empties the shared object. a and b share one Map. After a.clear(), b.size is also 0.
  2. new Map() only retargets one variable. c = new Map() points c at a fresh empty Map.
  3. The old Map keeps its data. d still points at the old Map with size 1, while c is empty.

Browser & Runtime Support

Map.prototype.clear() is part of ES2015 (ES6), together with WeakMap, Set, and WeakSet.

✓ Baseline · ES2015

Map.prototype.clear()

Works in every modern browser, plus Node.js, Deno, and Bun. Internet Explorer 11 has a basic Map with set(), get(), has(), delete(), clear(), and size.

98% Modern browser support
Google Chrome 36+ · Desktop & Mobile
Full support
Mozilla Firefox 6+ · Desktop & Mobile
Full support
Apple Safari 8+ · macOS & iOS
Full support
Microsoft Edge 12+
Full support
Node.js 0.12+ · every current release
Supported
Deno All releases
Supported
Internet Explorer IE 11 · no constructor argument
Basic
Map.prototype.clear() Excellent

Bottom line: Safe to use in any modern browser and runtime. clear() empties the same Map object and returns undefined everywhere.

Frequently Asked Questions

It removes every entry from a Map, so the size becomes 0. The Map object itself stays the same and can be used again. The method returns undefined.
clear() empties the same Map object, so every variable that points to it sees an empty Map. map = new Map() only points one variable at a new empty Map, and other variables keep the old one with its entries.
No. The entries are removed for good. If you may need them again, copy first with new Map(map), and use the copy later.
No. It returns undefined. To check the result, read map.size, which is 0 after the call.
It is shorter and clearer when you want an empty Map. For a normal-sized Map the speed difference does not matter, so choose clear() because it says exactly what you mean.

Next: keys()

Loop over every key in insertion order with keys().

keys() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful