Back to Lessons

JavaScript Map and Set

April 5, 2026

Map and Set Objects

Advanced collection types with better performance.

Map and Set Examples

// Map (key-value, any key type)
let map = new Map();
map.set("name", "John");
map.set(123, "number");
console.log(map.get("name")); // "John"

// Set (unique values)
let set = new Set([1, 2, 2, 3]); // {1, 2, 3}
set.add(4);
set.delete(2);

// Iteration
for (let [key, value] of map) {
    console.log(key, value);
}

Key Points

  • Map preserves insertion order.
  • Set automatically removes duplicates.
  • Keys can be objects/primitives in Map.
  • size, has(), clear() methods.