The Codex / JavaScript / Set & Map

Set & Map

JavaScript's modern collection types — Set for unique values, Map for key-value pairs with any key type.

What it is, in plain English: A Set is a collection of unique values — it automatically removes duplicates. A Map is a collection of key-value pairs where keys can be any type (not just strings like in plain objects). Both were added in ES2015 and are the right tool when you need uniqueness (Set) or a dictionary with non-string keys (Map).

Two modern collections: Set and Map

You already know arrays (ordered lists) and objects (key-value pairs with string keys). JavaScript has two more collection types that solve specific problems those don't handle well: Set for unique values, Map for flexible key-value storage.

Set — a collection where every value is unique

A Set is like an array, but it automatically refuses duplicates. If you add the same value twice, it only keeps one copy.

// Create a Set from an array — duplicates are removed automatically
const fruits = new Set(["apple", "banana", "apple", "cherry", "banana"]);
console.log(fruits);        // Set { "apple", "banana", "cherry" }
console.log(fruits.size);   // 3 — only 3 unique values

// Add values one at a time
fruits.add("date");
fruits.add("apple");   // already exists — ignored silently
console.log(fruits.size);  // 4

// Check membership — very fast (O(1)), unlike Array.includes()
console.log(fruits.has("banana"));  // true
console.log(fruits.has("mango"));   // false

// Remove a value
fruits.delete("banana");
console.log(fruits.has("banana"));  // false

// Loop over a Set
for (const fruit of fruits) {
  console.log(fruit);   // apple, cherry, date
}

// Convert back to an array with spread:
const uniqueArray = [...fruits];
console.log(uniqueArray);  // ["apple", "cherry", "date"]

The most common Set use case: deduplicating an array

const tags = ["javascript", "css", "javascript", "html", "css", "javascript"];
const uniqueTags = [...new Set(tags)];
console.log(uniqueTags);  // ["javascript", "css", "html"]

// Also fast membership check — better than Array.includes() for large lists:
const allowedRoles = new Set(["admin", "editor", "viewer"]);
const userRole = "admin";
if (allowedRoles.has(userRole)) {
  console.log("Access granted");
}

Map — key-value pairs with any key type

Objects store key-value pairs, but keys must be strings (or symbols). A Map allows any value as a key — numbers, objects, booleans, anything.

// Create a Map
const capitals = new Map([
  ["France", "Paris"],
  ["Japan",  "Tokyo"],
  ["India",  "New Delhi"],
]);

// Get, set, has, delete
console.log(capitals.get("Japan"));   // "Tokyo"
capitals.set("Brazil", "Brasília");
console.log(capitals.has("France"));  // true
capitals.delete("France");
console.log(capitals.size);           // 3

// Loop over entries — always in insertion order
for (const [country, capital] of capitals) {
  console.log(`${country}: ${capital}`);
}

// Get all keys / all values:
console.log([...capitals.keys()]);    // ["Japan", "India", "Brazil"]
console.log([...capitals.values()]);  // ["Tokyo", "New Delhi", "Brasília"]

When to use Map vs plain object

Plain object {}
Map
Key types
Strings and Symbols only
Any value (objects, numbers, functions...)
Order
Mostly insertion order (not guaranteed for all engines)
Always insertion order
Size
Object.keys(obj).length
map.size (direct, fast)
Best for
Static structure (user object, config, data shape)
Dynamic key-value store, counting, caching
// Map is better for dynamic counting:
const wordCount = new Map();
const words = ["the", "cat", "sat", "on", "the", "mat", "the"];
for (const word of words) {
  wordCount.set(word, (wordCount.get(word) ?? 0) + 1);
}
console.log(wordCount.get("the"));  // 3
console.log([...wordCount.entries()].sort((a,b) => b[1]-a[1]));
// [["the",3],["cat",1],["sat",1],["on",1],["mat",1]] — sorted by count
Try It Yourself

Official Sources

The Codex links only to official documentation and the ECMAScript specification. No third-party tutorials.