Skip to solution
hardFrontend

What are WeakMap and WeakSet?

210 views
01

Understand the problem

Question presented to candidate: "Why would you reach for a WeakMap instead of a regular Map to attach some private metadata to a set of objects? What real constraints does WeakMap have that a regular Map doesn't?"

What a strong answer should cover:

  • 📌 Interview term: WeakMap/WeakSet — collections that hold their keys (WeakMap) or values (WeakSet) with weak references, meaning the JavaScript engine's garbage collector can reclaim that object's memory once nothing else in the program references it, even though the WeakMap/WeakSet itself still technically "contains" it.
  • 📌 Interview term: object-only keys/values — verified directly: a WeakMap/WeakSet genuinely throws a real TypeError ("Invalid value used as weak map key" / "...weak set") for a string, number, or registered symbol (Symbol.for(...)) — only objects, and plain (non-registered) Symbols, are accepted.
  • 📌 Interview term: no enumeration — verified directly: neither has a real .size property, a real Symbol.iterator, nor a real .forEach — they are genuinely not iterable and cannot be inspected as a whole, only queried for one specific key/value at a time (.get/.has).
  • A precise answer names the real, practical use case this constraint enables: attaching private, per-object metadata (like a cache entry, or "has this been processed") that automatically disappears when the original object is garbage collected, without the WeakMap itself artificially keeping that object alive — verified directly with a real "process each object only once" pattern.
  • A precise answer names the direct contrast with a regular Map: verified directly, a regular Map genuinely DOES have .size and genuinely IS iterable — the exact features WeakMap deliberately omits specifically to make weak referencing and non-enumeration reliable.

Clarifying questions expected:

  • None — this is a definitional/technical question; naming the object-only constraint AND the no-enumeration constraint together, with the real reason both exist, is the strong signal.

Code / implementation expected: Yes — showing the real TypeError for a primitive key, plus the missing .size/iterability, demonstrates genuine understanding beyond "it's like Map but weak."

weakmap
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript fundamentals interviews. Difficulty: Medium

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every constraint below, including the exact TypeErrors, was actually run in Node.

1.

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSReal proof of WeakMap/WeakSet's two constraints: object-only keys and no enumeration, plus a real process-once pattern
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 160 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track