Skip to main content

Command Palette

Search for a command to run...

Map and Set in JavaScript

Updated
4 min read
Map and Set in JavaScript
S
A front-end developer who’s always learning, building projects, and writing blogs to simplify web concepts

Introduction

JavaScript’s default tools—Objects and Arrays—work well, but they start to break down in specific scenarios like key collisions and duplicate handling. Map and Set are designed to solve exactly those problems with cleaner semantics and better performance.


What is Map

A Map is a key–value data structure where keys can be of any type, not just strings.

const map = new Map()

map.set(1, "Anirban")
map.set("1", "Rahul")

console.log(map)

Here, 1 and "1" are treated as different keys. Unlike Objects, no implicit string conversion happens.

In practice, Map behaves more like a true dictionary or hash table. It preserves insertion order and provides predictable behavior when storing dynamic key-value data.


What is Set

A Set is a collection that stores only unique values.

const set = new Set()

set.add(1)
set.add(1)
set.add(2)

console.log(set)

Even though 1 is added twice, it appears only once. This makes Set ideal when your main concern is eliminating duplicates without writing extra logic.


The Problem with Objects

Objects seem like natural key-value stores, but they have a critical limitation.

const users = {}

users[1] = "Anirban"
users["1"] = "Rahul"

console.log(users)

The output ends up with only one key. This happens because object keys are internally converted to strings, causing collisions. This behavior can introduce subtle bugs, especially when working with numeric or mixed-type keys.

Map avoids this entirely by preserving the original key type.


The Problem with Arrays

Arrays are ordered collections, but they don’t enforce uniqueness.

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

If you want unique values, you have to manually filter or process the array. Also, checking whether a value exists requires scanning through elements, which is less efficient.

Set simplifies both concerns by handling uniqueness automatically and offering faster lookups.


Map vs Object

The difference is less about syntax and more about intent. Objects are general-purpose structures, while Map is purpose-built for key-value storage. With Map, you get consistent key handling, built-in size tracking, and better performance for frequent updates.

Objects still work well for static data structures or JSON-like representations, but once keys become dynamic or non-string, Map is the safer choice.


Set vs Array

Arrays are ideal when order and indexing matter. You can access elements by position and manipulate sequences easily. However, they allow duplicates and require extra work for membership checks.

Set, on the other hand, focuses purely on uniqueness. You don’t get indexing, but you gain simplicity when dealing with repeated values and faster existence checks.


When to Use Map

Use Map when the relationship between keys and values is central to your logic and key types are not strictly strings. It’s especially useful in caching, tracking objects as keys, or handling dynamic datasets where insertions and deletions are frequent.


When to Use Set

Use Set when your primary concern is maintaining a collection of unique values. It works well for removing duplicates, tracking visited items, or ensuring that a dataset does not repeat entries.


Visual Understanding

Map (Key → Value)

1    → "Anirban"
"1"  → "Rahul"
true → "Active"
{}   → "ObjectKey"

Set (Unique Values)

[1, 2, 3, 4]

Conclusion

Map and Set are not replacements for Objects and Arrays—they are specialized tools. Map gives you reliable key-value storage without hidden conversions, while Set ensures clean, duplicate-free collections. Choosing the right structure makes your code more predictable and easier to reason about.

JavaScript Journey: From Basics to Core Concepts

Part 26 of 29

This series documents my journey of learning JavaScript and breaking down important concepts in a simple way. Each article covers a core JavaScript topic with clear explanations and beginner-friendly examples. From basic concepts to essential JavaScript features, the goal of this series is to make JavaScript easier to understand while practicing and sharing what I learn.

Up next

Creating Routes and Handling Requests with Express

Introduction Once you understand how Node.js runs JavaScript and handles basic HTTP servers, the next step is making your code easier to manage. Writing everything using Node’s built-in http module qu