Map and Set in JavaScript

Whenever you need to store data in JavaScript, what is the first thing that comes to your mind? If you have a list of items, you probably use an Array. If you have data with labels (key-value pairs), you probably use an Object.
For years, Arrays and Objects were the only tools developers had to organize data. But as web applications became more complex, developers started running into frustrating limitations.
To solve these problems, JavaScript introduced two powerful new data structures in ES6: Map and Set.
In this blog, we will explore the problems with traditional Objects and Arrays, what Map and Set are, how they differ from their older counterparts, and exactly when you should use them in your own code.
The Problem with Traditional Objects and Arrays
Before we look at the solutions, we need to understand the problems.
The Array Problem: Arrays are fantastic for keeping a list of items in a specific order. However, arrays allow duplicate values. If you are building a social media app and want to keep a list of unique users who liked a post, an Array will happily let the same user be added 10 times. To prevent this, you have to write extra loops and conditional logic to check if a user already exists before adding them.
The Object Problem: Objects are the traditional way to store key-value data, like a user profile where name: "Suprabhat", age: 23. But Objects have a massive hidden flaw: Object keys can only be Strings or Symbols. If you try to use a number, a boolean, or even another object as a key, JavaScript will automatically convert it into a string. This can cause bizarre bugs and limits how you can organize complex data.
Let's see how Map and Set rescue us from these issues.
What is a Set?
A Set is a special type of collection that holds a list of values, just like an Array. But it has one strict, magical rule: Every value in a Set must be 100% unique.
You can never have duplicates in a Set. If you try to add a value that already exists, the Set simply ignores it. This "uniqueness property" makes Sets incredibly useful for filtering data.
Let's look at how easy it is to use a Set:
// Creating a new Set
const uniqueTags = new Set();
// Adding values
uniqueTags.add("javascript");
uniqueTags.add("web");
uniqueTags.add("javascript"); // This is a duplicate!
console.log(uniqueTags);
// Output: Set(2) { 'javascript', 'web' }
Notice how we tried to add "javascript" twice, but the Set only kept one copy.
A very common developer trick is to pass an Array with duplicates directly into a Set to instantly clean it up:
const messyArray = [1, 2, 2, 3, 3, 3, 4];
const cleanSet = new Set(messyArray);
console.log(cleanSet); // Output: Set(4) { 1, 2, 3, 4 }
Difference between Set and Array
While they look similar, they serve very different purposes.
| Feature | Array | Set |
|---|---|---|
| Duplicates | Allowed. | Strictly NOT allowed (Unique only). |
| Data Access | Accessed by index position (e.g., myArray[0]). |
No index. You check if a value exists using mySet.has(value). |
| Performance | Searching for an item is slower in large lists. | Checking if an item exists is incredibly fast. |
| Best For | Ordered data where position matters. | Storing unique values and checking for their existence. |
What is a Map?
A Map is a collection of keyed data items, just like an Object. It holds key-value pairs.
However, Map completely solves the biggest problem with Objects: A Map allows keys of ANY data type. Your key can be a string, a number, a boolean, a function, or even an entire object!
Let's look at an example. Imagine we want to store extra information about a specific user object without actually modifying the original object.
// Our traditional user object
const user1 = { name: "Suprabhat", age: 23 };
// Creating a new Map
const userRoles = new Map();
// We are using the ENTIRE user object as the key!
userRoles.set(user1, "Admin");
console.log(userRoles.get(user1)); // Output: "Admin"
If we tried to do this with a regular Object, JavaScript would convert the user1 object into a useless string "[object Object]". With Map, the original object is preserved securely as the key.
Additionally, Maps have built-in features that make them easier to work with, such as a .size property that instantly tells you how many items are inside (unlike Objects, where you have to manually count the keys).
Difference between Map and Object
Here is a quick comparison to help you choose the right tool for key-value storage.
| Feature | Object | Map |
|---|---|---|
| Key Types | Must be Strings or Symbols. | Can be absolutely anything (Objects, Arrays, Numbers). |
| Order of Items | Not guaranteed to be in the order you added them. | Strictly remembers the exact order of insertion. |
| Getting Size | Difficult. You must use Object.keys(obj).length. |
Easy. Just use the built-in myMap.size property. |
| Iteration | Requires extra methods like Object.entries(). |
Built natively for iteration using loops like for...of. |
When to use Map and Set
Now that we know how they work, when should you actually use them in your daily coding?
When to use Set:
Removing Duplicates: Whenever you receive messy data (like user inputs or database results) and need to ensure there are no repeating values.
Fast Lookups: If you have a massive list of ID numbers and you need to constantly check if a specific ID exists, using
mySet.has(id)is much faster than searching through an Array.Managing States: Keeping track of things that are either "on" or "off" (like a list of currently selected checkboxes).
When to use Map:
Complex Keys: When you need to associate data with a DOM element or another Object without modifying the original object.
Frequent Updates: Maps are highly optimized for scenarios where you are constantly adding and removing key-value pairs.
Predictable Iteration: When you need to loop through key-value data and absolutely need the items to appear in the exact order you added them.
Conclusion
Arrays and Objects will always be the foundational building blocks of JavaScript. You will still use them every single day.
However, Map and Set are highly specialized tools built for modern development. By understanding that Set is your go-to for guaranteed unique lists, and Map is your heavy-duty key-value storage that accepts any data type, you will write cleaner, faster, and much less buggy code.





