Map in JavaScript
Map is the key-value structure objects wish they were: any value (objects included) as keys with SameValueZero equality, insertion-order iteration, a real size property, and no prototype pollution (no hasOwnProperty collisions, no __proto__ weirdness). set/get/has/delete plus clear cover the API.
Choose Map over objects when keys aren't strings, when insertion order plus easy sizing matters, or when user-controlled keys could collide with Object.prototype. Objects remain right for fixed-shape records and JSON; Maps win for dynamic key collections.
Any keys, kept order
Objects, functions, NaN as keys — all distinct, all iterated insertion-first.
Real size
map.size reads directly — no Object.keys(m).length round-trips.
No prototype hazards
User keys like "constructor" or "__proto__" behave as data — impossible with plain objects.
Iterate naturally
for (const [k, v] of map) plus forEach/keys/values/entries — built for traversal.
let scores = new Map();scores.set("ada", 95);scores.set("bob", 80);console.log(scores.size, scores.get("ada"));console.log(scores.has("zoe"));Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in JavaScript yourself — building beats watching.