Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JavaScript · Class

WeakMap

WeakMap stores values using objects or non-registered symbols as keys without strongly retaining those keys. It is useful for object-associated data that should disappear as the key becomes unreachable.

How it works

A WeakMap stores values under object or non-registered symbol keys while avoiding strong references to those keys. It supports querying, retrieval, insertion, and deletion, but does not expose key enumeration because garbage collection can change which keys remain live.

Syntax

new WeakMap()
new WeakMap(iterable)

Key points

  • Keys are restricted to objects and non-registered symbols.
  • Values can be primitives, objects, functions, or any other JavaScript value.
  • Storing a key does not by itself prevent that key from being garbage collected.
  • When a key is collected, its corresponding WeakMap value can become eligible for collection if nothing else retains it.
  • WeakMap deliberately has no key-list or enumeration method.

Common mistakes

  • Primitive keys are generally invalid; the supported primitive exception is a non-registered symbol.

Example: Associating metadata with an object

const metadata = new WeakMap();

const user = {};
metadata.set(user, { role: "admin" });

console.log(metadata.get(user).role); // "admin"
console.log(metadata.has(user)); // true

// Once user is no longer referenced elsewhere, its entry can be collected.

A WeakMap associates metadata with an object without keeping that object alive solely because it is a key.

When to use it

Object-associated metadata

Attach metadata, caches, or private state to objects without making the objects stay alive solely because of that association.

Weakly retained caches

Keep computed information for object keys while allowing entries to become collectible when those keys are no longer used.

Encapsulated object state

Hide implementation details behind object identity rather than exposing an ordinary property on the object.

Interview questions

QWhat distinguishes WeakMap from Map?

WeakMap holds key/value pairs without strongly retaining its object keys. If a key becomes otherwise unreachable, it may be garbage collected, and its associated value can then become collectible too.

Follow-up: Why is WeakMap not iterable? Because garbage collection can remove keys at nondeterministic times, exposing enumeration would make the observable result depend on garbage-collection state.

QWhat keys and operations does WeakMap support?

WeakMap keys must be objects or non-registered symbols, while values may have any JavaScript type. Operations such as get, set, has, and delete work with a particular key, but there is no operation to list all keys.

Follow-up: When would a Map be preferable? Use Map when keys must be enumerable or when primitive keys such as strings and numbers are needed.

QWhen is WeakMap useful in application design?

A common use is attaching private or implementation-specific data to objects. The data remains associated with the object while it is reachable, without requiring a separate cleanup step when the object is collected.

Follow-up: How can object identity affect this pattern? A newly created object with the same properties is still a different key, so callers must retain and reuse the original reference.

  • Map
  • WeakSet
  • WeakMap.prototype.get()
  • WeakMap.prototype.set()
  • WeakMap.prototype.has()
  • WeakMap.prototype.delete()

Source: WeakMap - JavaScript - MDN Web Docs. Checked 2026-10-15.