Skip to main content

Command Palette

Search for a command to run...

Map and Set in JavaScript

Simple short beginner friendly explanation of Map and Set

Updated
5 min read
Map and Set in JavaScript

When working with data in JavaScript, developers commonly use Objects and Arrays. While they are useful, they have limitations when dealing with unique values, complex keys, or frequent updates.

To overcome these issues, JavaScript provides two powerful data structures:

  • Map

  • Set

This article explains both in a clear, practical way with real-life examples.

What is a Map?

A Map is a collection of key-value pairs, similar to an object, but more flexible. having more methods for the more control

Features

  • Keys can be of any data type objects, arrays, functions etc.

  • Maintains insertion order

  • Provides built in methods for better control

  • Easy to iterate

Basic Example

const user = new Map();

user.set("name", "Nikhil");
user.set("age", 22);

console.log(user.get("name")); // Nikhil
console.log(user.has("age"));  // true

Real-Life Example: User Session Storage

const sessions = new Map();

const user1 = { id: 1 };
const user2 = { id: 2 };

sessions.set(user1, "Session123");
sessions.set(user2, "Session456");

console.log(sessions.get(user1)); // Session123

Here, objects are used as keys, which is not possible with normal objects.

What is a Set?

A Set is a collection of unique values. So, there is no repetition it must be used for avoiding the reptation of data.

Features

  • Stores only unique values

  • Maintains insertion order

  • Fast lookup

  • Useful for filtering duplicate data

Basic Example

const numbers = new Set();

numbers.add(1);
numbers.add(2);
numbers.add(2); // Duplicate ignored

console.log(numbers); // {1, 2}

Real-Life Example: Unique Visitors

const visitors = new Set();

visitors.add("user1");
visitors.add("user2");
visitors.add("user1");

console.log(visitors.size); // 2

This is useful for tracking unique users or identifiers.

Map vs Object

Feature Map Object
Key Types Any data type can be used as key Strings and Symbols only
Order Maintains insertion order Not guaranteed (older JS)
Performance Better for frequent changes Slower for large data
Iteration Easy (for...of) Requires extra methods

Problem with Objects

const obj = {};
const key = { id: 1 };

obj[key] = "value";

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

Objects convert keys into strings, which can lead to unexpected behavior.

Set vs Array

Feature Set Array
Duplicates Not allowed Allowed
Search Faster Slower (includes)
Order Maintained Maintained
Use Case Unique data Ordered list

Problem with Arrays

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

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

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

Arrays allow duplicates, so additional logic is needed to clean data.

When to Use Map

Use Map when:

  • You need key-value pairs

  • Keys are not just strings

  • You frequently add or remove data

  • You need predictable iteration order

Common use cases:

  • Caching data

  • Managing user sessions

  • Mapping objects to values

When to Use Set

Use Set when:

  • You need unique values

  • You want to remove duplicates

  • You need fast existence checks

Common use cases:

  • Tracking unique users

  • Removing duplicates from arrays

  • Managing tags or categories

Map Structure & Set Structure

Key Takeaways

  • Map is an advanced key-value data structure with flexible keys

  • Set is designed to store unique values only

  • Map is ideal for structured data relationships

  • Set is ideal for maintaining clean, duplicate-free collections

summary

In JavaScript, while Objects and Arrays are commonly used for data management, they have limitations such as handling unique values, complex keys, and frequent updates. To address these issues, JavaScript offers two advanced data structures: Map and Set. A Map is a collection of key-value pairs that allows keys of any data type, maintaining insertion order and offering built-in methods for better control and easy iteration. It is particularly useful when keys are not just strings, such as in user session storage or caching data. Maps provide predictable iteration order and are efficient for frequent data updates.

On the other hand, a Set is a collection designed to store only unique values, making it ideal for avoiding duplicates and performing fast existence checks. Sets maintain insertion order and are efficient for filtering duplicate data, such as tracking unique visitors or managing tags. While Arrays allow duplicates and require additional logic to clean data, Sets inherently prevent repetition, simplifying the process of maintaining clean collections.

Overall, Maps are suited for scenarios requiring complex key-value relationships and frequent modifications, whereas Sets are perfect for managing collections of unique items. These structures enhance JavaScript's data handling capabilities, providing developers with more robust and efficient tools for managing complex data scenarios.