JavaScript · Map · Set · WeakMap · WeakSet

Map & Set,
finally understood

The underused siblings of Array and Object. Deduplication, fast lookups, frequency counts, and garbage-collection-safe caching — all animated with real scenarios.

map-set.js
// Map — any key type, ordered, O(1) lookup
const roles = new Map([
  ['alice', 'admin'],
  ['bob',   'editor'],
]);
roles.get('alice');  // 'admin'
roles.has('bob');    // true
roles.set('carol', 'viewer');

// Set — unique values, O(1) has()
const tags = new Set(['js','css','js']);
// Set {'js', 'css'}  ← deduped!

// Deduplicate array in one line
const unique = [...new Set(arr)];

5 real-world scenarios

All Map & Set methods

Map
new Map()map.set()map.get()map.has()map.delete()map.clear()map.sizemap.forEach()map.keys()map.values()map.entries()
Set
new Set()set.add()set.has()set.delete()set.clear()set.sizeset.forEach()set.keys()set.values()set.entries()
union()intersection()difference()symmetricDifference()isSubsetOf()isSupersetOf()isDisjointFrom()
WeakMap & WeakSet
new WeakMap()weakMap.get()weakMap.set()weakMap.has()weakMap.delete()new WeakSet()weakSet.add()weakSet.has()weakSet.delete()
View full reference →