Map and Set
Two built-in collections — Map for key-value pairs, Set for unique values — that improve on what plain objects and arrays can do.
What is it?
You've already used plain objects as key-value stores, and arrays as ordered lists. Map and Set are more specialized built-in collections that fix a few rough edges: a Map lets you use any value (not just strings) as a key and keeps track of its own size; a Set stores a collection of values with no duplicates allowed, automatically.
Explain like I'm 10
A Map is like a proper dictionary with tabs for any kind of entry — not just word-shaped ones. A Set is like a guest list where the bouncer automatically refuses to add the same name twice, no matter how many times you try.
Examples
Map and Set basics
const scores = new Map();
scores.set("amara", 90);
scores.set("diego", 85);
console.log(scores.get("amara")); // 90
console.log(scores.size); // 2
const uniqueNumbers = new Set([1, 2, 2, 3, 3, 3]);
console.log(uniqueNumbers.size); // 3
console.log(uniqueNumbers.has(2)); // trueRemoving duplicates from an array with Set
const numbers = [1, 2, 2, 3, 1, 4];
const unique = [...new Set(numbers)];
console.log(unique); // [1, 2, 3, 4]Spreading a Set back into an array is a common one-line pattern for deduplicating an array while preserving the first occurrence of each value.
How it works
A Map stores entries in insertion order and, internally, uses the same kind of fast key-based lookup a hash table does — but without a plain object's quirks (string-coerced keys, inherited properties getting in the way). A Set is really just a Map that only cares about the keys — adding a value that's already present is simply a no-op.
Why does it exist?
Plain objects were never really designed to be general-purpose maps — keys are always converted to strings, there's no built-in size, and inherited properties can sneak in unexpectedly. Map and Set exist specifically to be clean, purpose-built collections without that historical baggage.
When to use it
Reach for a Map when your keys aren't simple strings, when you need a reliable .size, or when insertion order matters and must be preserved. Reach for a Set whenever you need a collection of values with automatic deduplication — removing duplicates from an array, or tracking a group of unique items.
When not to use it
For a simple, small collection of string keys — especially one that's going to be serialized with JSON.stringify (which doesn't support Map/Set directly) — a plain object or array is often simpler and more familiar.
Common mistakes
Trying to
JSON.stringifya Map or Set directly and being surprised it doesn't serialize the way a plain object or array does.Using
.lengthon a Map or Set instead of the correct property,.size.Forgetting that Set only removes duplicate values — it doesn't otherwise change the order or type of the data.
Practice exercises
- Easy:
Use a
Setto remove duplicate values from an array of numbers. - Medium:
Use a
Mapto count how many times each word appears in a sentence, and log the results. - Hard:
Write a function that returns the intersection (common values) of two arrays, using Sets.
Interview questions
What's the main advantage of Map over a plain object for key-value storage?
Map allows any value as a key (not just strings), maintains a reliable .size, and doesn't risk inherited properties interfering with lookups.
How does Set handle duplicate values?
It silently ignores an attempt to add a value that's already present — a Set can only ever contain unique values.
Can you iterate over a Map in insertion order?
Yes — Maps (and Sets) always iterate in the order entries were inserted.