Set & Map
JavaScript's modern collection types — Set for unique values, Map for key-value pairs with any key type.
A Map is still an object, so square brackets still work on it, just not the way you'd hope. capitals["India"] = "New Delhi" doesn't add an entry to the Map; it sticks an ordinary property on the Map object. No error is thrown, but .get() can't find it, .size stays 0, and loops over the Map skip it.
const capitals = new Map();
capitals["India"] = "New Delhi"; // looks like it works...
console.log(capitals.get("India")); // ...but the Map never saw it
console.log(capitals.size);
// undefined
// 0Two 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"]Map entries live behind its methods, so every read and write goes through them, as in the examples above: .set(key, value) to add or update, .get(key) to read, .has(key) to check and .delete(key) to remove. Square brackets are for plain objects:
const capitals = new Map();
capitals.set("India", "New Delhi");
console.log(capitals.get("India"));
console.log(capitals.size);
// New Delhi
// 1When to use Map vs plain object
// 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 countOfficial Sources
- MDN Web Docs — Set
- MDN Web Docs — Map
- ECMAScript 2024 — Set Objects (§24.2)
- ECMAScript 2024 — Map Objects (§24.1)
- MDN Web Docs — WeakMap
The Codex links only to official documentation and the ECMAScript specification. No third-party tutorials.