JavaScript theorytheory 0/50 · 0%
Data · easy

18. Map, Set and WeakMap

Keyed collections beyond plain objects.

`Map` accepts any key type, preserves insertion order and reports `size`. `Set` stores unique values. `WeakMap` holds keys weakly so entries can be garbage collected — good for caches attached to objects.

const seen = new Set(["0xabc"]);
seen.has("0xabc"); // true

const nonces = new Map();
nonces.set(addr, 3n);

Use a Map when keys are dynamic or non-string; use an object for fixed, known shapes.

Check your understanding

  1. 1. What key types can a Map use?

  2. 2. What does a Set guarantee?

  3. 3. Why use a WeakMap?