JavaScript Intermediate

What are the differences between Set, Map, WeakSet, and WeakMap?

AI Practice

Set, Map, WeakSet, and WeakMap

Set

A collection of unique values:

const set = new Set([1, 2, 2, 3]);
set.size; // 3
const unique = [...new Set([1, 2, 2, 3])]; // [1, 2, 3]

Map

Key-value pairs where keys can be any type:

const map = new Map();
map.set('a', 1);
map.get('a'); // 1

WeakSet

  • Objects only
  • Weak references — allows GC
  • Not iterable, no size

WeakMap

  • Object keys only
  • Weak references to keys
  • Not iterable, great for private data

Comparison

Feature Set Map WeakSet WeakMap
Weak ref
Iterable
size

✦ AI Mock Interview

Type your answer and get instant AI feedback

Sign in to use AI scoring

Copyright © 2026 Wood All Rights Reserved · FE Interview Hub