Lesson 37 / 38

Set and Map

Two built-in collections beyond arrays and objects.

Set: unique values

A Set automatically drops duplicates and checks membership fast.

const tags = new Set(["js", "web", "js"]);
console.log(tags);              // Set(2) {'js', 'web'}
console.log(tags.has("web"));   // true
tags.add("css");

Map: any key type

Unlike a plain object, a Map's keys can be any type, including numbers and objects.

const scores = new Map();
scores.set("Ada", 92);
scores.set(1, "one");
console.log(scores.get("Ada"));  // 92
console.log(scores.size);        // 2

Map vs plain object

Unlike objects, Map keys can be any type, insertion order is always preserved, and .size gives the count directly — better for dynamic key-value data.

Iterating

Both are iterable directly with for...of: for (const [key, val] of map).