Map and Set in JavaScript

Learning web development in public. Writing simple, real-world explanations about web development concepts. Helping beginners understand why things work, not just how.
Search for a command to run...

Learning web development in public. Writing simple, real-world explanations about web development concepts. Helping beginners understand why things work, not just how.
No comments yet. Be the first to comment.
Every day, millions of users upload photos, videos, stories, and reels to Instagram. From a user's perspective, the process appears simple: select media, apply filters, add a caption, and tap "Post."
Building Offline-First Messaging Apps: How Messages Work Without Internet Modern messaging applications have transformed the way people communicate. Whether it's chatting with friends, collaborating w
In this article we'll explore about the Expo Router and React Navigation and answer which one to use in 2026. If you build mobile apps using React Native, one thing becomes obvious very quickly: Navig

Modern mobile apps are no longer just a collection of screens connected together. Apps like Instagram, WhatsApp, Uber, and Netflix operate at massive scale with millions of users, real time systems, o
In this article we'll be exploring react.js and the things of react.js that makes it popular and stand out among other libraries ( no fight over library vs framework ). We'll go through: What problem

Shkaai
68 posts
In this article, we’ll learn about two important data structures in JavaScript: Map and Set.
What we are going to learn:
What Map is
What Set is
Difference between Map and Object
Difference between Set and Array
When to use Map and Set
A Map is a collection of key-value pairs, just like an object — but more powerful and flexible.
The main difference is:
In a Map, keys can be of any type (not just strings).
const map = new Map();
map.set("name", "Josh");
map.set(1, "number key");
map.set(true, "boolean key");
console.log(map.get("name")); // Josh
console.log(map.get(1)); // number key
map.set(key, value) // add value
map.get(key) // get value
map.has(key) // check if key exists
map.delete(key) // remove key
map.clear() // remove all
A Set is a collection of unique values.
It automatically removes duplicates.
const set = new Set([1, 2, 2, 3, 4, 4]);
console.log(set); // {1, 2, 3, 4}
set.add(value) // add value
set.has(value) // check value
set.delete(value) // remove value
set.clear() // remove all
| Feature | Map | Object |
|---|---|---|
| Key type | Any type (number, object, boolean) | Only string or symbol |
| Order | Maintains insertion order | Not guaranteed (older behaviour) |
| Iteration | Easy with for...of |
Need extra methods |
| Performance | Better for frequent add/remove | Good for simple use cases |
const obj = {};
obj[1] = "one"; // converted to string "1"
const map = new Map();
map.set(1, "one"); // stays number
So Map keeps the original type of keys.
| Feature | Set | Array |
|---|---|---|
| Duplicates | Not allowed | Allowed |
| Order | Maintains order | Maintains order |
| Access | No index | Index-based |
| Use case | Unique values | Ordered list |
const arr = [1, 2, 2, 3];
const set = new Set(arr);
console.log(arr); // [1, 2, 2, 3]
console.log(set); // {1, 2, 3}
You need key-value pairs
Keys are not just strings (like numbers, objects)
You need better performance for frequent updates
Example:
const userRoles = new Map();
userRoles.set("user1", "admin");
userRoles.set("user2", "editor");
You need unique values only
You want to remove duplicates
You just care about existence (not position)
Example:
const numbers = [1, 2, 2, 3, 4];
const uniqueNumbers = new Set(numbers);
console.log(uniqueNumbers); // {1, 2, 3, 4}
Map is like an advanced object with more flexibility
Set is perfect when you only care about unique values
Both are very useful in real-world applications, especially when working with data.
Start using them in small projects, and you’ll quickly understand where they fit best.