Map
A Map is a collection of key-value pairs that remembers insertion order. Keys can be any value, including objects, and lookup is SameValueZero rather than string coercion.
Detailed Explanation
A plain object can store pairs, but its keys become strings or symbols. scores[1] and scores["1"] are the same property. An object also inherits toString and constructor, so untrusted keys can collide with those names. Map exists to be a dictionary without those rules.
new Map() is empty. new Map([[k, v], [k2, v2]]) takes an iterable of pairs. set, get, has, and delete are the core API. set returns the map, so you can chain. get returns undefined for a missing key, which means you cannot store undefined and tell it apart from a miss with get alone — use has.
Key equality is SameValueZero: like ===, except NaN matches NaN. Object keys are compared by reference. Two { id: 1 } literals are two keys. If you need to look up by contents, use a primitive key (user.id) or a canonical string.
Iteration is insertion order. map.keys(), map.values(), and map.entries() return iterators. for...of on a map yields [key, value] pairs. map.size is the number of keys. There is no length.
A Map is not JSON. JSON.stringify(new Map([["a", 1]])) is {}. Convert with Object.fromEntries(map) when keys are strings, or JSON.stringify([...map]) for an array of pairs.
What it costs you: a heavier structure than a small object, and no property syntax. map.foo is not a key. Engines optimise both; for string keys and a fixed shape, a plain object can still be faster. For frequent add/remove, mixed key types, or keys that are objects, Map is the right default.
Reach for Map when keys are not safe strings, when you need order, or when you set and delete often. Reach for a plain object when the shape is a record ({ email, id }) with known fields. Reach for WeakMap when keys are objects you do not want to keep alive.
Code Examples
const sessions = new Map();
const user = { id: 7 };
sessions.set(user, { token: "abc" });
sessions.set("guest", { token: "none" });
console.log(sessions.get(user)); // => { token: 'abc' }
console.log(sessions.has("guest")); // => true
console.log(sessions.size); // => 2
sessions.delete("guest");
console.log(sessions.size); // => 1const counts = new Map([
["ok", 2],
["err", 1],
]);
for (const [key, n] of counts) {
console.log(key, n);
}
console.log([...counts.keys()]); // => [ 'ok', 'err' ]
console.log(Object.fromEntries(counts)); // => { ok: 2, err: 1 }const m = new Map();
m.set(NaN, "not-a-number");
console.log(m.get(NaN)); // => not-a-number
m.set({ id: 1 }, "a");
console.log(m.get({ id: 1 })); // => undefined (different object)Common Gotchas
`get` cannot distinguish a stored `undefined` from a miss
call `has` first, or store a sentinel.
Object keys are references
a new object with the same fields is a different key. Use an id string when you mean identity by value.
`JSON.stringify` ignores Map contents
convert with `Object.fromEntries` or `[...map]` before you serialise.
`map.size` not `map.length`
`length` is `undefined` and will not throw.