Skip to main content

Command Palette

Search for a command to run...

Map and Set in JavaScript

Updated
โ€ข4 min read

As JavaScript evolved, developers needed better data structures than traditional objects and arrays for certain use cases.

Thatโ€™s where Map and Set come in.

They provide more control, better performance in some cases, and cleaner semantics.


๐Ÿ”น What is a Map?

๐Ÿ‘‰ A Map is a collection of key-value pairs, similar to objectsโ€”but with important differences.

๐Ÿ“Œ Example

const map = new Map();

map.set("name", "Sayantan");
map.set("age", 21);

console.log(map.get("name")); // Sayantan

๐Ÿ”น Key Features of Map

  • Keys can be any data type (not just strings)

  • Maintains insertion order

  • Has built-in methods like:

    • .set()

    • .get()

    • .has()

    • .delete()


๐Ÿ”น Example with Non-String Keys

const map = new Map();

map.set(1, "Number key");
map.set(true, "Boolean key");

console.log(map.get(1)); // Number key

๐Ÿ‘‰ This is NOT possible with normal objects.


๐Ÿ”น What is a Set?

๐Ÿ‘‰ A Set is a collection of unique values.

It automatically removes duplicates.


๐Ÿ“Œ Example

const set = new Set([1, 2, 2, 3, 4]);

console.log(set); // {1, 2, 3, 4}

๐Ÿ”น Key Features of Set

  • Stores only unique values

  • No duplicate entries

  • Maintains insertion order

  • Useful methods:

    • .add()

    • .has()

    • .delete()


๐Ÿ”น Example: Removing Duplicates

const arr = [1, 2, 2, 3];

const unique = [...new Set(arr)];

console.log(unique); // [1, 2, 3]

๐Ÿ”ฅ Map vs Object

Feature Map Object
Key Types Any type Strings/Symbols only
Order Maintains order Not guaranteed (historically)
Iteration Easy (for...of) Requires extra methods
Performance Better for frequent updates Good for simple use cases

๐Ÿ“Œ Problem with Objects

const obj = {};

obj[1] = "Number key";

console.log(obj["1"]); // "Number key"

๐Ÿ‘‰ Keys are converted to strings โ†’ loss of flexibility


๐Ÿ”ฅ Set vs Array

Feature Set Array
Duplicates Not allowed Allowed
Order Maintained Maintained
Access No index Index-based
Use Case Unique values Ordered data

๐Ÿ“Œ Problem with Arrays

const arr = [1, 2, 2, 3];

const unique = arr.filter((item, index) => arr.indexOf(item) === index);

console.log(unique); // [1, 2, 3]

โŒ Complex โŒ Inefficient

โœ” Set solves this easily


๐Ÿ”น When to Use Map

Use Map when:

  • You need key-value storage

  • Keys are not strings

  • Frequent additions/deletions

  • Need guaranteed insertion order


๐Ÿ“Œ Real Example

const userRoles = new Map();

userRoles.set("Sayantan", "Admin");
userRoles.set("Rahul", "User");

console.log(userRoles.get("Sayantan"));

๐Ÿ”น When to Use Set

Use Set when:

  • You need unique values

  • Removing duplicates

  • Checking existence quickly


๐Ÿ“Œ Real Example

const visitedPages = new Set();

visitedPages.add("/home");
visitedPages.add("/about");
visitedPages.add("/home");

console.log(visitedPages); // no duplicates

๐Ÿง  Mental Model

  • Map โ†’ advanced object (key-value storage)

  • Set โ†’ advanced array (unique values)


๐Ÿš€ Practical Use Cases

1. Remove Duplicates

const nums = [1, 2, 2, 3];

const uniqueNums = [...new Set(nums)];

2. Fast Lookup

const set = new Set([1, 2, 3]);

console.log(set.has(2)); // true

3. Dynamic Key Storage

const map = new Map();

const objKey = { id: 1 };

map.set(objKey, "Data");

console.log(map.get(objKey)); // Data

โš ๏ธ Common Mistakes

โŒ Using Object Instead of Map

const obj = {};

obj[{ id: 1 }] = "value";

console.log(obj); // "[object Object]"

๐Ÿ‘‰ Key gets stringified โ†’ incorrect behavior


โŒ Expecting Index in Set

const set = new Set([10, 20, 30]);

console.log(set[0]); // undefined โŒ

๐Ÿ‘‰ Sets donโ€™t support indexing

More from this blog

Sayantan Blogs

45 posts