You will be able to create a JavaScript Map, store and retrieve values with set and get, and iterate over its entries in insertion order.
What it is
A Map is a built-in JavaScript collection that stores key-value pairs. Unlike a plain object, a Map can use any value as a key, including objects, functions, and primitives, and it remembers the order in which entries were inserted. Related terms include set, get, has, delete, size, and iteration methods such as forEach, keys, values, and entries.
Why it matters
- It gives predictable insertion-order iteration.
- It avoids accidental prototype-chain lookups that can happen with plain objects.
- It supports non-string keys, such as DOM nodes or objects.
- It provides a clear API for checking, updating, and removing entries.
Syntax or steps
The smallest useful pattern is to create a Map, add entries with set, read entries with get, and loop through pairs with for...of.
const m = new Map();
m.set("a", 1);
m.set("b", 2);
m.get("a");
m.has("b");
m.delete("a");
m.size;
Example
const scores = new Map();
scores.set("Ada", 95);
scores.set("Grace", 98);
scores.set("Alan", 92);
console.log(scores.get("Grace"));
console.log(scores.has("Ada"));
for (const [name, score] of scores) {
console.log(name, score);
}
scores.forEach((score, name) => {
console.log(name, score);
});
First, new Map() creates an empty collection. set adds or updates a key-value pair. get returns the value for a key, or undefined if the key is missing. has checks whether a key exists. The for...of loop destructures each entry into name and score. The forEach callback receives value first, then key.
Common mistakes
- Using
scores["Ada"]instead ofscores.get("Ada"). Map entries are not normal object properties. - Assuming
getthrows when a key is missing. It returnsundefined, so usehasif you need to distinguish missing keys from storedundefined. - Using
for...into iterate a Map. Usefor...of,forEach, orentries. - Expecting object keys to be stringified. With a Map,
{}and"{}"are different keys.
When to use it
Use a Map when you need ordered key-value storage, non-string keys, or a collection that should not inherit object properties. Use a plain object when the data is a simple record with string or symbol keys and you want literal syntax.
| Need | Use |
|---|---|
| Insertion-order iteration | Map |
| Object or function keys | Map |
| Simple JSON-like record | Plain object |
| Weak references to objects | WeakMap |
Practice
- Guided: Create a Map called
prices, set three items, get one price, and log every item withfor...of. - Challenge: Add a method or loop that increases every price by 10, then print the updated Map. Hint: use
forEachorentriesandset.
Quick check
What does m.get("missing") return if "missing" was never set?
It returns undefined.
Summary
A JavaScript Map is an ordered key-value collection with explicit methods for getting, setting, checking, and iterating entries. Choose it over a plain object when key type, order, or clean iteration matter.