Map
Key-value collection with set, get, has, delete and size.
Map stores key-value pairs where keys can be any value. WeakMap stores object-keyed private metadata.
const counts = new Map();
counts.set("html", 3);
console.log(counts.get("html"));
Arrays & Collections
Objects are often used as dictionaries, but Map is built specifically for key-value storage with any key type.
Map preserves insertion order and gives you clear methods: set, get, has, delete and size.
WeakMap only accepts objects as keys. It is useful for private metadata connected to objects without preventing garbage collection.
Key-value collection with set, get, has, delete and size.
Map keys can be objects, functions or primitives.
Map is iterable in insertion order.
Object-keyed metadata without public iteration.
Examples
const countByStatus = new Map();
countByStatus.set("ready", 3);
countByStatus.set("draft", 1);
console.log(countByStatus.get("ready"));
const countByStatus = {};
countByStatus["ready"] = 3;
// This is fine for simple string keys, but Map is clearer for collection behavior.
Code patterns
These are the patterns developers search for constantly: reading items, copying arrays, choosing map/filter/reduce, sorting with a comparator, destructuring and using Set or Map.
Store and read a value by key.
const counts = new Map();
counts.set("html", 3);
console.log(counts.get("html"));
Use an object as a key.
const metadata = new Map();
const record = { id: 1 };
metadata.set(record, { selected: true });
Loop over key-value pairs.
for (const [key, value] of counts) {
console.log(key, value);
}
Attach metadata to objects privately.
const privateData = new WeakMap();
const record = {};
privateData.set(record, { visited: true });
console.log(privateData.get(record));
Rules that matter
Arrays and collections are not just storage. They describe whether the code is selecting, transforming, grouping, counting, sorting or enforcing uniqueness.
It gives clear collection methods and size.
Objects are great for records with known property names.
get can return undefined for missing keys.
Map entries are naturally iterable.
WeakMap keys must be objects.
WeakMap does not expose iteration or size.
Production thinking
Map separates dictionary-like collection behavior from ordinary object shape.
Map can track UI metadata by element or object without mixing it into visible data.
Production code should choose Map when keys are dynamic or not only strings.
Maps can group content safely before deterministic rendering, but final output should be arrays or strings.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
A collection method should tell the reader what kind of operation is happening before they read the callback body.
Chapter checkpoint
Finish this chapter by turning the lessons into a small practical proof.
Build a small example that combines three lessons from this chapter.
Can you explain the important tradeoff without reading from the page?