Skip to main content

Command Palette

Search for a command to run...

Map and Set in JavaScript

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

JavaScript gives us powerful data structures beyond traditional objects and arrays - Map and Set.

They solve real problems that developers often face with normal objects and arrays.

What Map is..

A Map is a collection of key-value pairs, just like objects, but with more flexibility and better performance in many cases.

Key Features of Map ->

  • Keys can be any data type (not just strings)

  • Maintains insertion order

  • Easy size tracking (map.size)

  • Built-in methods (set, get, has, delete)

Example

const userMap = new Map();

userMap.set('name', 'Janardan');
userMap.set(1, 'ID');
userMap.set(true, 'isLoggedIn');

console.log(userMap.get('name')); // Janardan
console.log(userMap.size); // 3

Think of Map like a dictionary where keys are not restricted to strings.

What Set is..

A Set is a collection of unique values only.

No duplicates allowed, that’s its superpower.

Key Features of Set ->

  • Stores only unique values

  • Maintains insertion order

  • Useful for removing duplicates

  • Built-in methods (add, has, delete)

Example

const numbers = new Set();

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

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

Think of Set like a filter that automatically removes duplicates.

Map vs Object

Feature Map Object
Key Types Any type Only string/symbol
Order Maintains insertion order Not guaranteed (older JS)
Performance Better for frequent additions/removals Slower in some cases
Size map.size Object.keys(obj).length
Iteration Easy (for...of) Needs extra methods

Problem with Objects

const obj = {};
obj[1] = 'one';
obj['1'] = 'string one';

console.log(obj); 
// { "1": "string one" } → key collision

Objects convert keys to strings -> unexpected bugs

But in Map

const map = new Map();

map.set(1, 'one');
map.set('1', 'string one');

console.log(map); // Map(2) { 1 => 'one', '1' => 'string one' }

Here, no collision, because both 1 is treat a different keys

Must remember thing

  • Object → "everything becomes string"

  • Map → "type-safe keys"

Set vs Array

Feature Set Array
Duplicates Not allowed Allowed
Search Faster (has) Slower (includes)
Indexing No index Yes
Use case Unique data Ordered list

Problem with Arrays

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

// Removing duplicates manually
const unique = [...new Set(arr)];

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

Arrays don’t enforce uniqueness -> extra logic needed.

When to Use Map

Use Map when ->

  • You need key-value pairs

  • Keys are not strings (objects, functions, etc.)

  • Frequent add/remove operations

  • You need ordered data

Example

const cache = new Map(); 
cache.set(userObject, responseData);

When to Use Set

Use Set when ->

  • You need unique values

  • Removing duplicates

  • Fast existence checks

  • Tracking visited items (e.g., graphs)

Example

const visited = new Set();

visited.add(node);
if (visited.has(node)) {
  console.log('Already visited');
}

One line Statement

Map = Advanced Object

Set = Unique Array