Skip to main content

Command Palette

Search for a command to run...

Map and Set in JavaScript

Learn how JavaScript Map and Set work, how they differ from objects and arrays, and when to use them effectively.

Updated
•6 min read•View as Markdown
Map and Set in JavaScript

Introduction

JavaScript provides several ways to store data.

Most beginners start with:

  • Arrays

  • Objects

But modern JavaScript also includes two powerful data structures:

  • Map

  • Set

These structures solve problems that traditional arrays and objects sometimes handle poorly.

In this article, you'll learn:

  • What Map is

  • What Set is

  • Map vs Object

  • Set vs Array

  • When to use them

  • Real-world examples

Why JavaScript Introduced Map and Set

Before Map and Set, developers mostly used:

  • Objects for key-value storage

  • Arrays for collections

But these had limitations.

For example:

  • Objects were not designed for advanced key management

  • Arrays allowed duplicate values easily

  • Checking uniqueness required extra logic

Map and Set were introduced to solve these issues more cleanly.

What is a Map?

A Map is a collection of:

key → value

pairs.

It stores data similarly to objects but with more flexibility.

Example:

const userMap = new Map();

userMap.set("name", "John");
userMap.set("age", 25);

console.log(userMap.get("name"));

Output:

John

How Map Works

A Map stores:

  • Keys

  • Associated values

Example visualization:

"name" → "John"
"age"  → 25

Creating a Map

Example:

const map = new Map();

Adding Values to Map

Use:

set()

Example:

map.set("city", "Delhi");

Getting Values from Map

Use:

get()

Example:

console.log(map.get("city"));

Checking if a Key Exists

Use:

has()

Example:

console.log(map.has("city"));

Removing Values from Map

Use:

delete()

Example:

map.delete("city");

What Makes Map Special?

Unlike objects, Map:

  • Maintains insertion order

  • Accepts any datatype as keys

  • Has built-in utility methods

  • Performs well for frequent additions/removals

Objects vs Map

Traditional Object

const user = {
  name: "John"
};

Objects work well for simple structures.

But they have limitations.

Map Example

const map = new Map();

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

Maps allow:

  • Number keys

  • Boolean keys

  • Object keys

  • Function keys

Map vs Object Comparison

Feature Object Map
Key Types Strings/Symbols Any datatype
Order Preservation Limited Yes
Built-in Methods Fewer More
Iteration Less convenient Easier
Performance for dynamic data Moderate Better

What is a Set?

A Set is a collection of:

unique values

It automatically removes duplicates.

Example:

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

console.log(numbers);

Output:

Set(3) {1, 2, 3}

Notice:

2

appears only once.

Why Set is Useful

Normally, arrays allow duplicates.

Example:

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

Removing duplicates manually requires extra logic.

Set handles uniqueness automatically.

Creating a Set

Example:

const set = new Set();

Adding Values to Set

Use:

add()

Example:

set.add("apple");
set.add("banana");

Checking Values in Set

Use:

has()

Example:

console.log(set.has("apple"));

Removing Values from Set

Use:

delete()

Example:

set.delete("banana");

Array vs Set

Array

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

Duplicates allowed.


Set

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

Duplicates removed automatically.

Set vs Array Comparison

Feature Array Set
Duplicate Values Allowed Not allowed
Index Access Yes No
Order Preservation Yes Yes
Searching Slower in large data Faster
Unique Data Handling Manual Automatic

Real-World Use Cases of Map

Map is useful for:

  • User sessions

  • Caching

  • Dynamic configurations

  • Key-value storage systems

  • Fast lookup operations

Example:

const userRoles = new Map();

userRoles.set("John", "Admin");
userRoles.set("Alice", "Editor");

Real-World Use Cases of Set

Set is useful for:

  • Removing duplicates

  • Tracking unique visitors

  • Unique tags/categories

  • Preventing repeated values

Example:

const uniqueEmails = new Set();

uniqueEmails.add("a@gmail.com");
uniqueEmails.add("a@gmail.com");

Only one email gets stored.

Removing Duplicates Using Set

A very common trick:

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

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

console.log(unique);

Output:

[1, 2, 3]

Iterating Over Map

Example:

const map = new Map([
  ["name", "John"],
  ["age", 25]
]);

for (const [key, value] of map) {
  console.log(key, value);
}

Iterating Over Set

Example:

const set = new Set(["apple", "banana"]);

for (const item of set) {
  console.log(item);
}

Common Beginner Mistakes

Expecting Set to Allow Duplicates

Example:

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

Output contains only:

1

Using Map Like an Object

Wrong:

map.name = "John";

Correct:

map.set("name", "John");

Map Key-Value Storage Visual

Map
 ├── "name" → "John"
 ├── "age"  → 25
 └── true   → "active"

Set Uniqueness Representation

Input:
[1, 2, 2, 3, 3]

Set:
{1, 2, 3}

When Should You Use Map?

Use Map when:

  • You need flexible keys

  • Frequent additions/removals happen

  • Order matters

  • You want efficient key-value management

When Should You Use Set?

Use Set when:

  • You need unique values

  • Duplicate prevention matters

  • Fast existence checking is required

Why Developers Love Map and Set

Map and Set make modern JavaScript:

  • Cleaner

  • Faster

  • More organized

They solve common problems elegantly without writing extra logic.

These structures are widely used in:

  • React

  • Node.js

  • Modern frontend and backend applications

Conclusion

Map and Set are powerful modern JavaScript data structures.

Map

Best for:

  • Flexible key-value storage

Set

Best for:

  • Unique value collections

Compared to traditional objects and arrays, they provide:

  • Better readability

  • Cleaner logic

  • More efficient operations

Understanding Map and Set helps you write smarter and more modern JavaScript applications.