Set: a collection of unique values
new Set(iterable), add, has, delete, size, clear
A set keeps each value once and checks membership very quickly.
const s = new Set([1, 2, 2, 3]);
s.add(4);
s.add(4);
console.log(s, s.size);
console.log(s.has(2), s.has(9));
s.delete(1);
console.log([...s]);
s.clear();
console.log(s.size);
Output:
Set(4) { 1, 2, 3, 4 } 4
true false
[ 2, 3, 4 ]
0
Removing duplicates
Spread a set back into an array.
console.log([...new Set(["a", "b", "a", "c", "b"])]);
console.log(new Set("mississippi").size);
Output:
[ 'a', 'b', 'c' ]
4
Looping over a Set
Items come out in the order they were added.
const s = new Set(["x", "y"]);
for (const v of s) console.log(v);
s.forEach((v) => console.log("each", v));
console.log([...s.entries()]);
Output:
x
y
each x
each y
[ [ 'x', 'x' ], [ 'y', 'y' ] ]
Set maths: union, intersection, difference
Built from spread and filter (newer engines also have s.union() etc.).
const a = new Set([1, 2, 3, 4]);
const b = new Set([3, 4, 5]);
console.log([...new Set([...a, ...b])]);
console.log([...a].filter((x) => b.has(x)));
console.log([...a].filter((x) => !b.has(x)));
Output:
[ 1, 2, 3, 4, 5 ]
[ 3, 4 ]
[ 1, 2 ]
Map: key-value pairs with any kind of key
new Map(), set, get, has, delete, size
Unlike an object, a Map's keys can be anything (numbers, objects), it keeps insertion order, and it has a real size.
const m = new Map();
m.set("a", 1).set(2, "two").set(true, "yes");
console.log(m.get("a"), m.get(2), m.get("zz"));
console.log(m.has(true), m.size);
m.delete("a");
console.log(m);
Output:
1 two undefined
true 3
Map(2) { 2 => 'two', true => 'yes' }
Creating from entries, and looping
Pass an array of pairs. Loop with for...of and destructuring.
const m = new Map([["Ram", 80], ["Sita", 92]]);
for (const [name, mark] of m) console.log(name, mark);
console.log([...m.keys()], [...m.values()]);
console.log(Object.fromEntries(m));
Output:
Ram 80
Sita 92
[ 'Ram', 'Sita' ] [ 80, 92 ]
{ Ram: 80, Sita: 92 }
Counting with a Map
The classic counter.
const counts = new Map();
for (const ch of "hello") counts.set(ch, (counts.get(ch) ?? 0) + 1);
console.log(counts);
console.log([...counts].sort((a, b) => b[1] - a[1])[0]);
Output:
Map(4) { 'h' => 1, 'e' => 1, 'l' => 2, 'o' => 1 }
[ 'l', 2 ]
Object keys versus Map keys
An object turns every key into text; a Map keeps the real key.
const o = {}; o[1] = "a"; o["1"] = "b";
console.log(o, Object.keys(o));
const m = new Map(); m.set(1, "a"); m.set("1", "b");
console.log(m.size);
const key = { id: 1 };
const byObject = new Map([[key, "data"]]);
console.log(byObject.get(key), byObject.get({ id: 1 }));
Output:
{ '1': 'b' } [ '1' ]
2
data undefined
WeakMap and WeakSet
Like Map and Set but they hold objects weakly: when nothing else refers to an object, it can be garbage-collected. They cannot be looped over. Use them to attach private data to objects without causing memory leaks.
WeakMap example
Keys must be objects.
const secret = new WeakMap();
const user = { name: "Sita" };
secret.set(user, "token-123");
console.log(secret.get(user), secret.has({}));
Output:
token-123 false
Choosing
| Need | Use |
|---|---|
| A list of unique values | Set |
| Fast "have I seen this?" | Set |
| Fixed, known property names (a record) | plain object |
| Many keys added and removed, or non-text keys, or you need the size | Map |
| Data that goes to JSON | object or array (a Map needs converting) |
Try it yourself
- Find the letters two words share, using sets.
- Check whether an array has any duplicate with a
Setandsize. - Use a Map to cache the result of a slow function.
Next lesson: functions.

Comments
Loading comments…