Map & Set
Built-in collection types
// Map: key-value pairs (any key type)
const map = new Map();
map.set("name", "Alice");
map.set(42, "the answer");
map.set(true, "yes");
for (const [key, value] of map) {
console.log(`${key} => ${value}`);
}
console.log("size:", map.size);
// Set: unique values
const nums = [1, 2, 2, 3, 3, 3, 4];
const unique = [...new Set(nums)];
console.log("Unique:", unique);
// Set operations
const a = new Set([1, 2, 3, 4]);
const b = new Set([3, 4, 5, 6]);
const intersection = [...a].filter(x => b.has(x));
const union = new Set([...a, ...b]);
console.log("Intersection:", intersection);
console.log("Union:", [...union]);