Think of a secret notebook where you can write notes about people, but you don’t keep their real names—just references to them (like pointing at them). If the person disappears (no one is referencing them), their note magically disappears from your notebook too. That’s what a WeakMap does in JavaScript.


What is a WeakMap?

A WeakMap is like a Map (a collection of key-value pairs), but:

  • The keys must be objects (not strings or numbers).
  • If the object is gone (no one references it), the WeakMap entry disappears automatically.

This means the data doesn’t cause memory leaks because JavaScript cleans it up for you when the object is no longer used.


Why is it called “Weak”?

Because the keys are weakly referenced—JavaScript doesn’t prevent them from being removed by the garbage collector. In a normal Map, if you store an object as a key, it stays forever unless you delete it. In a WeakMap, if the object disappears, the entry disappears too.


Example

let secretInfo = new WeakMap();

let user = { name: "Alice" };

secretInfo.set(user, "Likes pizza");

console.log(secretInfo.get(user)); // Likes pizza

user = null; // Remove reference to the object
// Now WeakMap entry is gone automatically!

Here:

  • We associate "Likes pizza" with user.
  • When user is set to null, the WeakMap automatically deletes the data.

What can you do with WeakMap?

  • .set(key, value) → Add a value for a key.
  • .get(key) → Get the value for a key.
  • .has(key) → Check if key exists.
  • .delete(key) → Remove key-value pair.

Where is it useful?

  • Storing private data for objects.
  • Attaching metadata to DOM elements without causing memory leaks.

Real-Life Analogy

Think of a VIP list at a secret club, where:

  • People (objects) can enter the club and have special info (values).
  • If someone leaves forever, their name is removed automatically.
  • You can’t see the full list, only check if someone is on it.

In Short

  • WeakMap = Map for objects only, with automatic cleanup.
  • Prevents memory leaks.
  • Keys are weakly held, so they disappear when the object is gone.

Review Questions

  1. A WeakMap stores data as __________ pairs.
  2. The keys in a WeakMap must be __________.
  3. If the object key is no longer referenced, the entry in WeakMap __________.
  4. WeakMap helps prevent __________ leaks.
  5. To add a value in WeakMap, we use the __________ method.
  6. To retrieve a value, we use the __________ method.
  7. To check if a key exists in WeakMap, we use the __________ method.
  8. WeakMap is useful for storing __________ data for objects.
  9. A real-life analogy for WeakMap is a __________ list at a secret club.
  10. Unlike a normal Map, WeakMap does not prevent the object from being __________ collected.

<
Previous Post
JavaScript .postMessage() and MessageEvent: Why do we need it?
>
Next Post
JavaScript WeakSet: Your Secret Guest List