JavaScript11 min read

JavaScript Set and Map Data Structures

Master Set and Map in JavaScript. Learn when to use them instead of arrays and objects.

Alex Thompson
Dec 20, 2025
13.1k522

JavaScript Set and Map

Set: Unique Values

Set stores unique values. No duplicates allowed.

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

numbers.add(5);
numbers.has(3); // true
numbers.delete(2);

Use when: You need unique values

Map: Key-Value Pairs

Map is like object but with any key type.

const map = new Map();
map.set('name', 'John');
map.set(1, 'One');
map.set(true, 'Yes');

map.get('name'); // "John"
map.has('name'); // true

Use when: You need keys that aren't strings

Key Takeaway

Set stores unique values. Map stores key-value pairs with any key type. Use when arrays/objects aren't enough.

#JavaScript#Set#Map#Data Structures#Intermediate