Map and Set in JavaScript - A Story of Choosing the Right Tool

Imagine you are building a simple system for tracking your college event. As usually primarily you use objects & arrays to store data. For a while everything works perfectly fine. But as soon as the project grows, you can see some issues in handling those data. Let's see what happened.
Day 1 - Starting with Objects
You stored student data in objects
const students = {
id1: "Riya",
id2: "Arjun"
};
console.log(students.id1); // Riya
No issues till now. Simple structure. So, you started using objects everywhere.
Day 2 - An Unexpected Problem
You want to track attendance , but you have to use actual student objects as key.
const riya = { name: "Riya" };
const arjun = { name: "Arjun" };
const attendance = {};
attendance[riya] = "Present";
attendance[arjun] = "Absent";
console.log(attendance);
You must have expected two separate entries, but both keys turned into
"[object Object]" & one value overwritten the other.
This is not a bug, this is a behaviour of object.
Object keys are always converted to strings
Day 3 - Discovering Map
You researched a bit about the problem and found Map.
const attendance = new Map();
const riya = { name: "Riya" };
attendance.set(riya, "Present");
console.log(attendance.get(riya)); // Present
Now everything working exactly as expected. There is no conversion , no overwritting.
Why Map Changes Everything
Let's understand why Map solved the problem:
const data = new Map();
data.set(1, "number key");
data.set("1", "string key");
console.log(data.get(1)); // number key
console.log(data.get("1")); // string key
In objects, both keys would collide, while in Map they stay completely separate.
Map always keeps key types intact.
Important Map Methods (Quick Reference)
These are the core tools you’ll actually use:
map.set(key, value); // add or update
map.get(key); // retrieve value
map.has(key); // check existence
map.delete(key); // remove entry
map.size; // total entries
Adding Another Feature: Event Check-ins
Now you wanted to build another feature that tracks how many times each student checks in.
const checkins = new Map();
checkins.set("Riya", 2);
checkins.set("Arjun", 1);
checkins.set("Riya", 3); // updated value
console.log(checkins.get("Riya")); // 3
Iteration Made Predictable
for (let [name, count] of checkins) {
console.log(name, count);
}
Map always maintains insertion order, so iteration behaves exactly as expected.
Day 4 - Another Problem of Duplicates
You started building a feature to track visitors. You started storing in an array.
const visitors = ["Riya", "Arjun", "Riya", "Kabir"];
But if one visitor comes more than onces it makes duplicate instances. To solve it either you had to write extra logic or manual filter, both would be uneccessary complexity.
Entry of Set
You researched to solve this issue, and found Set.
const visitors = new Set(["Riya", "Arjun", "Riya", "Kabir"]);
console.log(visitors); // {Riya, Arjun, Kabir}
Set removes all the duplicated automatically.
Real Scenario — Login Tracking
const loggedInUsers = new Set();
loggedInUsers.add("user1");
loggedInUsers.add("user2");
loggedInUsers.add("user1");
console.log(loggedInUsers.size); // 2
Even if a user logs in multiple times, they’re stored only once.
Set vs Array (The Practical Difference)
Array mindset:
Store everything → clean later
Set mindset:
Store only unique values from the beginning
Map vs Object (What You Actually Experienced)
Objects:
Keys become strings
Can overwrite unexpectedly
Not ideal for dynamic data
Maps:
Keys can be any type
No collisions
Maintain insertion order
Designed for frequent updates
Real-World Patterns You’ll Actually Use
Removing duplicates:
const tags = ["js", "react", "js"];
const uniqueTags = [...new Set(tags)];
Caching API data:
const cache = new Map();
cache.set("user_101", { name: "Riya" });
Tracking active sessions:
const sessions = new Set();
sessions.add("session_1");
When Should You Use Map?
Keys are not just strings
You need structured key-value storage
Data updates frequently
When Should You Use Set?
You need only unique values
You want fast duplicate removal
You need quick existence checks
Quick way to remember:
use Map for relationships ( key -> value) & set for uniqueness





