Map and Set

ES6 introduced Map and Set as alternatives to plain objects and arrays. If you are coming from Java or C++, these will feel familiar. They work similarly to HashMap/Map and HashSet/Set in those languages.

Map

A Map is a collection of key-value pairs where keys can be any type, not just strings like in regular objects.

Creating and Using Maps

const map = new Map();

// Adding entries with set()
map.set("name", "Alice");
map.set(1, "one");
map.set(true, "yes");

// Objects can be keys too
const objKey = { id: 1 };
map.set(objKey, "object value");

// Getting values with get()
console.log(map.get("name")); // "Alice"
console.log(map.get(1)); // "one"
console.log(map.get(objKey)); // "object value"

// Check size
console.log(map.size); // 4

You can also initialize a Map with an array of key-value pairs:

const map = new Map([
  ["name", "Bob"],
  ["age", 25],
  ["city", "NYC"],
]);

Map Methods

Method Description
set(key, value) Adds or updates an entry
get(key) Returns the value for a key
has(key) Returns true if key exists
delete(key) Removes an entry
clear() Removes all entries
size Property returning entry count
const scores = new Map();
scores.set("Alice", 95);
scores.set("Bob", 87);

console.log(scores.has("Alice")); // true
console.log(scores.has("Charlie")); // false

scores.delete("Bob");
console.log(scores.size); // 1

scores.clear();
console.log(scores.size); // 0

Map vs Object

Feature Map Object
Key types Any type Strings and Symbols
Size map.size Object.keys(obj).length
Default keys None Has prototype keys
Order Insertion order guaranteed Mostly preserved

Use Map when:

  • Keys are not strings (numbers, objects, etc.)
  • You need to know the size easily
  • You frequently add or remove entries

Use Object when:

  • Keys are strings
  • You need JSON serialization
  • You are working with a fixed structure

Set

A Set is a collection of unique values. Adding a duplicate value has no effect.

Creating and Using Sets

const set = new Set();

// Adding values
set.add(1);
set.add(2);
set.add(3);
set.add(2); // Duplicate - ignored

console.log(set.size); // 3
console.log(set.has(2)); // true
console.log(set.has(5)); // false

Initialize with an array (duplicates are automatically removed):

const set = new Set([1, 2, 3, 2, 1]);
console.log(set.size); // 3

Set Methods

Method Description
add(value) Adds a value
has(value) Returns true if value exists
delete(value) Removes a value
clear() Removes all values
size Property returning value count
const tags = new Set();
tags.add("javascript");
tags.add("programming");
tags.add("javascript"); // Ignored - already exists

console.log(tags.has("javascript")); // true
console.log(tags.size); // 2

tags.delete("programming");
console.log(tags.size); // 1

Removing Duplicates from an Array

One of the most common uses of a Set is removing duplicates from an array:

const numbers = [1, 2, 2, 3, 3, 3, 4];
const uniqueSet = new Set(numbers);
const unique = Array.from(uniqueSet);
console.log(unique); // [1, 2, 3, 4]

Set vs Array

Feature Set Array
Duplicates Not allowed Allowed
Value lookup O(1) with has() O(n) with includes()
Order Insertion order Index-based order
Access by index Not supported Supported

Use Set when:

  • You need unique values only
  • You frequently check if a value exists
  • Order does not matter or insertion order is fine

Use Array when:

  • You need duplicates
  • You need index-based access
  • You need array methods like map, filter, reduce

What is Next

This page covers the basics of Map and Set. We will come back to both in later chapters and cover more:

  • Iteration patterns with for...of and forEach
  • Converting between collections and arrays
  • Set operations (union, intersection, difference)
  • WeakMap and WeakSet for memory-efficient patterns