Map and Set in JavaScript

Problems with traditional objects and arrays
Objects and arrays cover most everyday needs — but they each have quiet limitations that eventually surface in real code. Object keys are silently converted to strings, even if you meant to use a number or an object as a key. Arrays don’t enforce uniqueness at all — checking for and removing duplicates means writing extra logic every time. Map and Set, both introduced in ES6, exist specifically to solve these two gaps.
What Map Is
A Map is a key-value data structure, similar in spirit to a plain object — but with genuinely flexible keys, and behavior specifically designed for this exact use case.
const userRoles = new Map();
userRoles.set("aarav", "admin");
userRoles.set("priya", "editor");
console.log(userRoles.get("aarav")); // "admin"
console.log(userRoles.size); // 2
Any type can be a key
const map = new Map();
const userObject = { id: 1 };
map.set("stringKey", "a string key");
map.set(42, "a number key");
map.set(userObject, "an object as a key!");
console.log(map.get(userObject)); // "an object as a key!"
This is something a plain object simply can’t do — object keys get silently converted to strings, but a Map keeps the key exactly as it was given, whatever type it is.

What Set Is
A Set is a collection of values where every value is automatically unique — adding a duplicate value simply has no effect.
const numbers = new Set([1, 2, 2, 3, 1]);
console.log(numbers); // Set(3) { 1, 2, 3 }
console.log(numbers.size); // 3 — duplicates were automatically removed
const uniqueNames = new Set();
uniqueNames.add("Aarav");
uniqueNames.add("Priya");
uniqueNames.add("Aarav"); // ignored — already exists
console.log(uniqueNames); // Set(2) { 'Aarav', 'Priya' }

This automatic uniqueness is Set’s entire reason for existing — with a plain array, checking for and removing duplicates requires writing that logic yourself, every time.
Difference Between Map and Object

const obj = {};
obj[42] = "value";
console.log(Object.keys(obj)); // ["42"] — the number became a string
const map = new Map();
map.set(42, "value");
console.log(map.get(42)); // "value" — still a real number key
Difference Between Set and Array

const array = [1, 2, 2, 3];
console.log(array); // [1, 2, 2, 3] — duplicates stay
const set = new Set([1, 2, 2, 3]);
console.log(set); // Set(3) { 1, 2, 3 } — duplicates removed automatically
A common practical use: removing duplicates from an array
const numbers = [1, 2, 2, 3, 3, 3, 4];
const unique = [...new Set(numbers)];
console.log(unique); // [1, 2, 3, 4]
Converting an array to a Set and back is one of the most common, genuinely useful one-line patterns for deduplicating an array in JavaScript.

When to Use Map and Set
Use Map when:
- Keys aren’t simple strings — numbers, objects, or other values need to be used as keys directly
- The key-value collection changes frequently (lots of adding and removing)
- You need a reliable size, or guaranteed iteration order
- You’re modeling something genuinely dynamic, like a cache or a lookup table built at runtime
Use Set when:
- You need to guarantee a collection has no duplicate values
- You need fast existence checks (.has()) on a potentially large collection
- You’re deduplicating an existing array
- Order matters less than the guarantee of uniqueness itself
When plain objects and arrays are still the right choice
For simple, mostly-static, string-keyed data (like a single user object with name, age, email), a plain object remains simpler and more natural. For ordered collections where duplicates are expected and index-based access matters, a plain array is still the right tool — Map and Set solve specific gaps, not a wholesale replacement for either.

Final Takeaway
Map and Set exist to close two specific, real gaps left by plain objects and arrays: Map gives you genuinely flexible keys (any type, not just strings) with a purpose-built API for a collection that changes often; Set gives you automatic, enforced uniqueness, without needing to write your own duplicate-checking logic. Neither replaces objects or arrays outright — for simple, everyday data, objects and arrays remain the more natural, familiar choice. But the moment you need non-string keys, guaranteed order, or a collection that simply can’t contain duplicates, Map and Set are built specifically for exactly that.
Frequently Asked Questions
Can I convert a Map back into a plain object, or a Set back into an array?
> Yes — Object.fromEntries(map) converts a Map into a plain object (assuming string-compatible keys), and [...set] (using the spread operator) converts a Set into a regular array, as shown in the deduplication example above.
Does a Set maintain insertion order?
> Yes — like Map, a Set preserves the order values were added in, which is different from historically inconsistent guarantees around plain object key ordering in some edge cases.
Is Map always faster than a plain object?
> Not universally — for frequent additions and removals of key-value pairs, Map is generally optimized for that pattern. For simple, mostly-static objects, the difference is often negligible, and object syntax remains more familiar and readable.
Can a Set contain objects, or only primitive values?
> A Set can contain any value, including objects — but note that two separate objects with identical properties are still considered different, unique values (since Set uniqueness is based on reference equality for objects, the same way === comparison works).
Originally published by Mr Madhukar
Read the complete article on Medium with full formatting & reader responses.