Map and WeakMap
Keyed data, done right.
A plain object pretends to be a dictionary, but its keys are always strings and it carries inherited baggage. Map is the real thing: any value as a key, honest size, guaranteed insertion order β and WeakMap attaches private metadata without leaking memory.
Why not just use an object?
An object literal is the reflex for keyed data, and for small static configs it's fine. But its keys are coerced to strings, so obj[1] and obj['1'] collide, and you can't key by an object or function at all. A Map keeps keys at their original type and compares them with SameValueZero β the same rule a Set uses. It also tracks its own size, so you never reach for Object.keys(obj).length.
Any value as a key
The headline feature: map.set(keyObject, value) lets you key by object identity. Two distinct objects with identical contents are still two different keys, which is exactly what you want when associating metadata with a specific instance β a DOM node, a request, a config object β rather than with its serialised form.
Insertion order is guaranteed
A Map iterates entries in the order they were first inserted, full stop. Plain objects mostly do too, but with a notorious exception: integer-like string keys are reordered to numeric ascending order. Map has no such rule, so it's the safe choice when order is part of your contract.
WeakMap: metadata without leaks
A WeakMap only accepts objects as keys, and it holds those keys weakly β once nothing else references the key object, the entry is eligible for garbage collection. That makes it the canonical tool for caching per-object data or stashing "private" state outside the object itself, with zero risk of pinning memory alive. The trade-off: a WeakMap is not iterable and has no size, because its contents can vanish at any time.
Try it 6 examples
Object keys vs Map keys
JSintroThe object stores only one entry (Object.keys(obj).length is 1) because the numeric key 1 was coerced to the string '1' and overwrote it, while the Map keeps both as distinct keys and reports size 2 β proof that Map preserves key type.