# Map, Set, WeakMap & WeakSet

> Move beyond standard Objects and Arrays. Learn how to store unique values with Sets and map complex keys with Maps.

Subject: JavaScript | Lesson 28 of 63 | 8 min read
Interactive lesson: https://codewithanimation.in/javascript/map-set-weakmap
Previous: [Array Iteration: map, filter, reduce](https://codewithanimation.in/javascript/array-iteration-methods.md) | Next: [JSON: parse & stringify](https://codewithanimation.in/javascript/json.md)

## Advanced Data Structures

While Arrays and Objects are the workhorses of JavaScript, ES6 introduced four new collection types for specialized tasks: `Map`, `Set`, `WeakMap`, and `WeakSet`.

Unlike Arrays (which are ordered lists) or Objects (which map strings to values), these structures offer unique memory and iteration constraints.

## Set (Unique Values)

A `Set` is a collection of values where every item must be **completely unique**. If you attempt to `.add()` a value that already exists, the Set simply ignores it.

Because Sets enforce uniqueness, they are the fastest and easiest way to remove duplicates from an array: `const unique = [...new Set([1, 1, 2])]`.

## Map (Any Key Type)

A `Map` is a key-value store similar to an Object. However, standard Objects only allow strings or Symbols as keys. A Map allows **anything** to be a key - including functions, arrays, or other objects!

Maps also preserve insertion order perfectly and have a built-in `.size` property, making them much easier to iterate over than standard Objects.

## Common mistake: The Weak Variants

`WeakMap` and `WeakSet` are identical to their counterparts, with one major catch: they only accept Objects, and they hold them 'weakly'. If the rest of your application forgets about an object stored in a WeakMap, the garbage collector will delete it from memory automatically. They are primarily used to prevent memory leaks in large applications.

## Tip: Methods to Remember

Maps use `.set(k, v)`, `.get(k)`, and `.has(k)`. Sets use `.add(v)`, `.has(v)`, and `.delete(v)`. Both use `.clear()` to empty the collection and `.size` to check length.

## Try it: Collection Architecture

Examine the structural differences between an Object, a Map, and a Set to understand when to use each.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/map-set-weakmap

## Check yourself

### Question 1

What is the primary difference between a Map and a standard Object?

- A. A Map allows any data type to be used as a key, while Objects only allow strings and symbols.
- B. A Map is much faster at sorting data than an Object.
- C. Objects use methods like .set() and .get(), while Maps use dot notation.

Correct answer: A

Standard Objects force all keys to become strings. Maps maintain the exact data type of the key, allowing you to use DOM nodes or objects as keys!

- Why not B: Incorrect. Maps preserve insertion order, but they are not inherently 'sorting' mechanisms.
- Why not C: Incorrect. You have it backward. Maps use `.set()` and `.get()`.

### Question 2

How many items will this Set contain? `new Set([1, 2, 2, 3, 1])`

- A. 3
- B. 5
- C. 0

Correct answer: A

A Set enforces uniqueness. It will keep `1, 2, 3` and quietly discard the duplicate `2` and `1`.

- Why not B: Incorrect. Sets automatically drop duplicate values.
- Why not C: Incorrect. It will keep the first instance of each unique value.

### Question 3

Why would you use a WeakMap instead of a standard Map?

- A. To allow the garbage collector to free up memory if the key objects are no longer needed.
- B. To store strings and numbers using less memory.
- C. To easily iterate over data in a random order.

Correct answer: A

WeakMaps hold 'weak' references to their keys. If a key object is deleted everywhere else in your app, the WeakMap lets it be garbage collected, preventing memory leaks.

- Why not B: Incorrect. WeakMaps cannot use strings or numbers as keys at all.
- Why not C: Incorrect. WeakMaps are fundamentally not iterable.

## Remember this

- `Set` is a collection of unique values. It automatically ignores duplicates.
- Passing an array into `new Set(arr)` is the easiest way to deduplicate it.
- `Map` is a key-value store that allows ANY data type to be used as a key (even objects).
- Maps preserve insertion order and provide a direct `.size` property.
- `WeakMap` and `WeakSet` prevent memory leaks by allowing their contents to be garbage collected.
