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 used plain objects as key-value stores and arrays as ordered lists. Map and Set are built-in collections that fix some rough edges:
- A
Mapaccepts any value (not just strings) as a key and tracks its own size. - A
Setholds values with duplicates removed automatically.
Explain like I'm 10
A Map is like a proper dictionary with tabs for any kind of entry, not just words. A Set is like a guest list where the bouncer refuses to add the same name twice, however often 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-liner to remove duplicates while keeping the first occurrence of each value.
How it works
A Map keeps entries in insertion order and uses fast key-based lookup, like a hash table, without a plain object's quirks (keys turned into strings, inherited properties getting in the way). A Set is essentially a Map that only cares about keys: adding a value that's already there does nothing.
Why does it exist?
Plain objects weren't designed as general-purpose maps: keys are always converted to strings, there's no built-in size, and inherited properties can sneak in. Map and Set are purpose-built collections without that baggage.
When to use it
Use a Map when keys aren't simple strings, when you need a reliable .size, or when insertion order must be kept. Use a Set when you need unique values: removing duplicates from an array, or tracking a group of unique items.
When not to use it
For a small collection of string keys, especially one you'll serialize with JSON.stringify (which doesn't support Map/Set directly), a plain object or array is often simpler.
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 a Set only removes duplicates. 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?
Adding a value that's already there is silently ignored. A Set only ever holds unique values.
Can you iterate over a Map in insertion order?
Yes. Maps (and Sets) always iterate in the order entries were inserted.