Skip to main content

Command Palette

Search for a command to run...

Map & sets in JavaScript

Updated
4 min read
Map & sets in JavaScript
D
CS student focused on backend & OS fundamentals. Building real projects and sharing practical learning.

befro these javascript only hava

  • objects -> key-value storage

  • Arrays -> ordered list

Problem with object

at that time objects were never design as pure key value database

issues:

  • Keys are always strings (or forced into strings)

  • No reliable ordering guarantee (older JS especially)

  • Prototype pollution risk (__proto__ issues)

  • Hard to measure size (Object.keys(obj).length)

Problem with Arrays:

Arrays are simple, but:

  • Duplicates are allowed (no uniqueness enforcement)

  • Searching is slow (O(n) for includes/filter)

  • Not meant for “unique collections”

So JS needed better structures.

That’s where map and set come in.


Map

it is better way to store aa key value data

same as object

key - value

but structurally

Basic example

const map = new Map();

map.set("name", "Dushyant");
map.set("age", 21);

console.log(map.get("name")); // Dushyant

so whats happen here

instead of this

const obj = {
name: "Dushyant"
};

we do this

map.set("name", "Dushyant"

core feature

  1. keys can be any type
map.set(1, "one");        // number key
map.set(true, "yes");     // boolean key
map.set({}, "object");    // object key
  1. clean method

map gives you direct built in functionnn to do a common task

add value

obj.name = "Dushyant";

Get value:

obj.name

Check if key exists:

"name" in obj

Get size:

 Object.keys(obj).length

Set

set is colletio of values where duplicate are not allowed

example

const set = new Set();

set.add(1);
set.add(2);
set.add(2); // duplicate

console.log(set);

output

{1, 2}

the second 2 was ignored

set automatically remove dupicates

why set exist

for a array

it allow duplication now you got the condition you want a unique value and you dont want to manually filter every time

thats where sert come in

core feature

1. No duplicates (MOST IMPORTANT)

set.add(5);
set.add(5);

Still only one 5

2. Simple methods

set.add(1);      // add
set.has(1);      // check
set.delete(1);   // remove
set.size;        // total

Same idea like Map → clean and direct

3. Remove duplicates from array

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

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

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

This is where Set is actually used in real projects


Map vs Object

1. Keys

Object:

const obj = {};
obj[1] = "one";

console.log(obj); // { "1": "one" }

Key becomes string

Map:

const map = new Map();
map.set(1, "one");

console.log(map.get(1)); // one

Key stays number

  • Object → only string/symbol keys

  • Map → any type (number, boolean, object, etc.)

2. Purpose (this is what really matters)

Object:

Used for structured data

const user = {
  name: "Dushyant",
  age: 20
};

Map:

Used for dynamic key value storage.

map.set(userId, data);

Conclusion:

  • Object = data structure

  • Map = data storage tool


Set vs Array

1. Duplicates

Array:

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

Duplicates allowed (no restriction)

Set:

const set = new Set([1, 2, 2, 3]);
console.log(set); // {1, 2, 3}

Duplicates automatically removed

Main difference:

  • Array → allows duplicates

  • Set → only unique values

2. Searching

Array:

arr.includes(3); // checks one by one

Slower for large data

Set:

set.has(3);

Faster and direct

3. Purpose

Array:

Used for:

  • ordered data

  • lists

  • applying methods like map, filter, reduce

Set:

Used for:

  • uniqueness

  • removing duplicates

  • fast existence check

4. Methods

Array:

arr.push(4);
arr.pop();
arr.map();
arr.filter();

Lots of methods (for data processing)

Set:

set.add(4);
set.delete(2);
set.has(1);

Limited, focused on uniqueness


when to use set and map

Understanding Map and Set is one thing. Knowing where to use it important .

When to use Map

use map when:

  • Keys are not just strings (numbers, objects, etc.)

  • You need frequent add/remove operations

  • You want a clean and consistent way to manage data

When to use Set

Use Set when your main goal is to store unique values only .

Use Set when:

  • You want to remove duplicates

  • You need fast existence checks

  • You don’t care about duplicate entries