Map and Set in JavaScript ποΈβ‘
Understanding Better Data Structures in Modern JavaScript

π Introduction
In JavaScript, we usually use:
Arrays for lists
Objects for key-value pairs
But these traditional structures have limitations.
π Duplicate values π Limited key flexibility π Unexpected behavior in some cases
To solve this, JavaScript introduced:
Map πΊοΈ
Set π¦
Letβs understand them simply.
π§ What is a Map?
A Map is:
A collection of key-value pairs where keys can be any type.
π§© Example:
const map = new Map();
map.set("name", "Tony");
map.set(1, "number key");
map.set(true, "boolean key");
console.log(map);
π§ Key Idea:
π Unlike objects, Map allows:
Objects as keys
Numbers as keys
Boolean keys
π Map Key-Value Visualization
Key β Value
"name" β "Tony"
1 β "number key"
true β "boolean key"
π§ What is a Set?
A Set is:
A collection of unique values
π§© Example:
const set = new Set();
set.add(10);
set.add(20);
set.add(20);
set.add(30);
console.log(set);
π¦ Output:
Set {10, 20, 30}
π§ Key Idea:
π Set automatically removes duplicates
π Set Uniqueness Visualization
Input: 10, 20, 20, 30
β
Set: 10, 20, 30
βοΈ Map vs Object
| Feature | Map | Object |
|---|---|---|
| Key types | Any type | Strings/symbols only |
| Order | Maintains insertion order | Not guaranteed |
| Size | .size |
Manual count |
| Performance | Better for frequent updates | Standard use case |
π§ When Map is Better:
π When keys are dynamic or non-string π When frequent additions/removals happen
βοΈ Set vs Array
| Feature | Set | Array |
|---|---|---|
| Duplicates | Not allowed | Allowed |
| Lookup speed | Fast | Slower |
| Order | Maintained | Maintained |
| Methods | add/delete/has | push/pop/filter |
π§ When Set is Better:
π When you need unique values only
π Map Example (Real Use Case)
const userRoles = new Map();
userRoles.set("admin", ["create", "delete"]);
userRoles.set("user", ["read"]);
console.log(userRoles.get("admin"));
π§ Use Case:
Role-based access systems
Caching data
Dynamic key storage
π Set Example (Real Use Case)
const uniqueUsers = new Set();
uniqueUsers.add("Alice");
uniqueUsers.add("Bob");
uniqueUsers.add("Alice");
console.log(uniqueUsers);
π§ Use Case:
Removing duplicates
Tracking unique visitors
Filtering data
π Traditional Object vs Map Problem
β Object Limitation:
const obj = {};
obj[1] = "one";
obj[{a:1}] = "object key";
console.log(obj);
π Object keys become strings internally
β Map Fixes This:
const map = new Map();
map.set({a:1}, "object key works");
console.log(map);
π§ Why Set is Powerful
Without Set:
const arr = [1, 2, 2, 3, 3];
const unique = [...new Set(arr)];
π Still possible, but Set makes it direct and efficient
π― Key Takeaways
Map = key-value storage with flexible keys
Set = collection of unique values
Map is better than Object for dynamic keys
Set is better than Array for uniqueness
Both improve performance and code clarity
βοΈ Practice Challenge
Try this:
π Create a Set from an array with duplicates π Create a Map storing user roles π Compare both with Object and Array
π₯ Conclusion
Map and Set are powerful additions to JavaScript.
π Map gives flexible key-value storage π Set ensures uniqueness with simplicity
Together, they help you write cleaner, more efficient, and more modern JavaScript code πͺ
Happy Coding β‘β¨



