Skip to main content

Command Palette

Search for a command to run...

Understanding Map and Set in JavaScript

Updated
5 min read

JavaScript provides several built-in data structures for storing and managing data. Traditionally, developers relied on objects and arrays for most tasks. However, modern JavaScript introduced two powerful structures: Map and Set.

These structures solve some limitations of objects and arrays and provide more flexibility when working with collections of data.

In this article, we will cover:

  • What Map is

  • What Set is

  • The difference between Map and Object

  • The difference between Set and Array

  • When to use Map and Set


Problems with Traditional Objects and Arrays

Before understanding Map and Set, it helps to see some limitations of objects and arrays.

Objects

Objects are often used for storing key-value pairs.

Example:

const user = {
  name: "Dipan",
  age: 21
};

However, objects have some limitations:

  • Keys are automatically converted to strings

  • Objects are not designed specifically for key-value collection operations

  • Iteration over objects is less straightforward


Arrays

Arrays are useful for storing lists of values.

Example:

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

However, arrays allow duplicate values, which may not always be desirable.

Example:

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

Sometimes we need a structure where each value is unique, and this is where Set becomes useful.


What Is a Map?

A Map is a data structure used to store key-value pairs, similar to objects, but with more flexibility.

Example:

const userMap = new Map();

userMap.set("name", "Dipan");
userMap.set("age", 21);

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

Output:

Dipan

Key features of Map:

  • Keys can be any data type

  • Maintains insertion order

  • Designed specifically for key-value storage


Map Example

const map = new Map();

map.set("city", "Kolkata");
map.set("country", "India");

console.log(map);

Retrieving values:

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

Difference Between Map and Object

Feature Object Map
Key types Strings or symbols Any data type
Order Not guaranteed Maintains insertion order
Built for key-value storage General structure Designed specifically for it
Size property Not directly available map.size

Example showing flexible keys:

const map = new Map();

map.set(1, "number key");
map.set(true, "boolean key");
map.set({ id: 1 }, "object key");

Objects cannot easily support such flexible keys.


What Is a Set?

A Set is a collection of unique values.

This means duplicate values are automatically removed.

Example:

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

console.log(numbers);

Output:

Set(4) {1, 2, 3, 4}

Even though 2 appeared twice, the set only stores it once.


Set Example

Adding values to a set:

const set = new Set();

set.add(1);
set.add(2);
set.add(2);
set.add(3);

console.log(set);

Removing values:

set.delete(2);

Checking existence:

console.log(set.has(1));

Difference Between Set and Array

Feature Array Set
Duplicate values Allowed Not allowed
Access by index Yes No
Main use Ordered list Unique collection

Example comparison:

Array

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

Duplicates remain.


Set

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

Duplicates are removed automatically.


Practical Use Cases

Removing Duplicate Values

Sets are commonly used to remove duplicates.

Example:

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

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

console.log(uniqueNumbers);

Output:

[1, 2, 3, 4]

Storing Key-Value Data Efficiently

Maps are useful when working with dynamic key-value data.

Example:

const userRoles = new Map();

userRoles.set("admin", "full access");
userRoles.set("editor", "edit content");
userRoles.set("viewer", "read only");

When to Use Map and Set

Use Map when:

  • You need flexible key-value storage

  • Keys are not limited to strings

  • You frequently add or remove entries


Use Set when:

  • You need unique values

  • You want to remove duplicates easily

  • You need fast existence checks


Conclusion

Map and Set are powerful data structures introduced in modern JavaScript that provide better ways to manage collections of data.

Key points to remember:

  • Map stores key-value pairs with flexible key types

  • Set stores unique values only

  • Map is more flexible than objects for key-value storage

  • Set is useful for handling collections without duplicates

  • Both structures solve common limitations of traditional objects and arrays

Understanding when and how to use Map and Set will help you write more efficient and organized JavaScript code.