Learn JavaScript
❯ collections.map_collection

Map in JavaScript

$concept

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.

$how it works
1

Any keys, kept order

Objects, functions, NaN as keys — all distinct, all iterated insertion-first.

2

Real size

map.size reads directly — no Object.keys(m).length round-trips.

3

No prototype hazards

User keys like "constructor" or "__proto__" behave as data — impossible with plain objects.

4

Iterate naturally

for (const [k, v] of map) plus forEach/keys/values/entries — built for traversal.

$example
let scores = new Map();
scores.set("ada", 95);
scores.set("bob", 80);
console.log(scores.size, scores.get("ada"));
console.log(scores.has("zoe"));
Output
2 95
false
1Two entries set; size reads 2 directly.
2get retrieves ada's 95; has reports zoe absent (false).
3Dictionary semantics with none of objects' key-type restrictions.
$use cases
Dynamic key collections (caches, indexes, registries)
Non-string keys (objects, functions) in lookups
User-controlled keys safe from prototype pollution
Insertion-ordered iteration with direct sizing

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in JavaScript yourself — building beats watching.