Map and Set in JavaScript

Published: (April 4, 2026 at 01:46 AM EDT)
3 min read
Source: Dev.to

Source: Dev.to

What Is a Map?

A Map is a collection of key‑value pairs, similar to an object, but with several improvements:

  • Keys can be any type (objects, functions, primitives)
  • Preserves insertion order
  • Built‑in methods to get, set, delete, and check entries

Map Example

const map = new Map();

map.set("name", "Rahul");
map.set("age", 22);
map.set(true, "Boolean key");

console.log(map.get("name")); // Rahul
console.log(map.has("age"));  // true
console.log(map.size);        // 3

Map Visual

Key → Value
"name"  → "Rahul"
"age"   → 22
true    → "Boolean key"

Map vs Object

FeatureMapObject
Key typesAny typeString / Symbol
Order preservationYesNot guaranteed
Size propertymap.sizeMust compute manually
IterationBuilt‑inManual / Object.keys

What Is a Set?

A Set is a collection of unique values—duplicates are automatically removed.

  • Can store any type of value
  • Provides fast lookups and uniqueness checks

Set Example

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

console.log(set);          // Set(3) { 1, 2, 3 }
console.log(set.has(2));   // true

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

Set Visual

[1, 2, 3, 2, 1] → Set → {1, 2, 3}

Set vs Array

FeatureSetArray
Uniqueness✅ Unique values❌ Allows duplicates
Lookup speedFastLinear search
Methodsadd, delete, haspush, pop, includes

When to Use Map and Set

Use Map when:

  • You need dynamic keys of any type
  • You want ordered key‑value storage
  • You need built‑in iteration over entries

Use Set when:

  • You want unique values only
  • You need to quickly check existence
  • You want to remove duplicates from an array

Removing Duplicates Example

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

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

Map for Counting Occurrences

const arr = ["apple", "banana", "apple"];
const countMap = new Map();

arr.forEach(item => {
  countMap.set(item, (countMap.get(item) || 0) + 1);
});

console.log(countMap);
// Map(2) { "apple" => 2, "banana" => 1 }

Key Takeaways

  • Map → flexible key‑value storage, ordered, keys can be any type
  • Set → collection of unique values, fast lookups, removes duplicates easily
  • Both solve common limitations of objects and arrays.
0 views
Back to Blog

Related posts

Read more »

TypeScript Type Guards

When you're building a payment system, “close enough” isn’t good enough A single undefined value or a mismatched object property can be the difference between...

Spread vs Rest Operators in JavaScript

!Cover image for Spread vs Rest Operators in JavaScripthttps://media2.dev.to/dynamic/image/width=1000,height=420,fit=cover,gravity=auto,format=auto/https%3A%2F%...

Destructuring in JavaScript

Have you ever written code like this? js // repetitive extraction const numbers = 10, 20, 30; const first = numbers0; const second = numbers1; It works—but it’s...

Synchronous vs Asynchronous JavaScript

JavaScript is single‑threaded, yet it can handle multiple tasks efficiently by using synchronous and asynchronous behavior. What Is Synchronous Code? Synchronou...