Map and Set are ES6 collections designed for fast key/value lookups and unique-value membership checks. Learn when to use Map.set/get/has, how Set deduplicates values, and common pitfalls like confusing Map.get(undefined) with missing keys.
What are Map and Set in JavaScript, and how does get() work?
The core idea
Map and Set are modern JavaScript collections:
- Map: stores key -> value pairs.
- Set: stores unique values only.
If your interview answer is one sentence: use Map for lookup tables and Set for fast membership checks and deduplication.
Collection | Stores | Key operations | Typical use case |
|---|---|---|---|
| Key-value pairs |
| Caching, indexing by id, metadata by object key |
| Unique values |
| Deduping arrays, visited-node tracking, permission checks |
const userScores = new Map();
userScores.set('u1', 82);
userScores.set('u2', 91);
console.log(userScores.get('u1')); // 82
console.log(userScores.has('u3')); // false
const activeUsers = new Set();
activeUsers.add('u1');
activeUsers.add('u1'); // duplicate ignored
activeUsers.add('u2');
console.log(activeUsers.size); // 2
console.log(activeUsers.has('u2')); // true
Important detail about Map.get()
get() returns undefined for a missing key, but it also returns undefined if the key exists and its value is explicitly undefined.
So in critical code, pair get() with has().
const settings = new Map();
settings.set('theme', undefined);
console.log(settings.get('theme')); // undefined
console.log(settings.get('language')); // undefined
console.log(settings.has('theme')); // true
console.log(settings.has('language')); // false
Equality rule used by Map and Set
Map and Set compare keys/values using SameValueZero:
NaNis considered equal toNaN.0and-0are treated as the same.- Object keys are compared by reference identity, not by deep value.
const m = new Map();
m.set(NaN, 'not-a-number');
console.log(m.get(Number('foo'))); // 'not-a-number'
const a = { id: 1 };
const b = { id: 1 };
m.set(a, 'first');
console.log(m.get(a)); // 'first'
console.log(m.get(b)); // undefined (different object reference)
High-impact real-world patterns
1) Deduplicate incoming IDs with Set before making API calls.
2) Index entities by id in Map for O(1) average lookups.
3) Track visited states in graph/DFS/BFS with Set.
const ids = ['p1', 'p2', 'p1', 'p3'];
const uniqueIds = [...new Set(ids)];
// ['p1', 'p2', 'p3']
const products = [
{ id: 'p1', name: 'Keyboard' },
{ id: 'p2', name: 'Mouse' }
];
const byId = new Map(products.map((p) => [p.id, p]));
console.log(byId.get('p2')?.name); // 'Mouse'
Common interview pitfalls
- Confusing
Array.prototype.map()withMapthe collection type. - Using objects as Map keys and expecting deep-value matching.
- Assuming
map.get(key)can distinguish missing key vs undefined value withouthas(). - Using plain objects for heavy insertion/deletion workloads where Map is clearer.
Practical scenario
An autocomplete feature receives repeated product IDs from multiple endpoints. You can dedupe IDs with Set, then create a Map for instant id -> product lookup during rendering.
Common pitfalls
- Using array
includesrepeatedly in hot paths instead of a Set membership check. - Using object property access for dynamic keys that are not strings.
- Forgetting insertion-order behavior when rendering deterministic lists.
Benchmark with realistic payload sizes. Add tests for duplicate values,
undefined map values, and object-key identity behavior.Use Map when you need key-value lookups with any key type and clean APIs like set/get/has. Use Set when you care about uniqueness and fast membership checks. In production code, combine get() with has() whenever undefined can be a valid stored value.
Use the relevant interview-question hub first, then move into a concrete study plan before targeted company sets.