Map & Set in JavaScript

From the JavaScript cheat sheet · Objects & Arrays · verified Jul 2026

Map & Set

Key-value collections and unique value sets beyond plain objects and arrays

javascript
// Map — key-value pairs (any key type)
const map = new Map();
map.set("name", "Alice");
map.get("name");  // "Alice"

// Set — unique values only
const set = new Set([1, 2, 2, 3]);
// Set { 1, 2, 3 }
💡 Map accepts ANY key type (objects, functions, numbers) — Object only accepts strings/symbols
⚡ [...new Set(array)] is the fastest way to deduplicate an array
📌 Map preserves insertion order and has .size — Object needs Object.keys().length
🟢 Use Map for dynamic key-value data; use Object for structured data with known properties
mapsetcollections

More JavaScript tasks

Back to the full JavaScript cheat sheet