Skip to main content

Command Palette

Search for a command to run...

Map and Set in JavaScript πŸ—‚οΈβš‘

Understanding Better Data Structures in Modern JavaScript

Updated
β€’4 min read
Map and Set in 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 ⚑✨

How the Web Works πŸ•ΈοΈπŸ•ΈοΈ

Part 5 of 50

A practical web development series explaining how the web worksβ€”from DNS and browsers to servers, HTTP, APIs, and deploymentβ€”while clearly connecting these fundamentals to real-world website programming using frontend and backend examples.

Up next

Why Node.js is Perfect for Building Fast Web Applications βš‘πŸš€

Understanding What Makes It So Efficient