Sets
A Set is a collection with one defining rule: every value in it is unique. Add a duplicate and nothing happens — it's already there.
let numbers = new Set([1, 2, 3, 2, 1]); console.log(numbers); numbers.add(4); console.log(numbers); console.log(numbers.has(2)); console.log(numbers.size);
Set { 1, 2, 3 }
Set { 1, 2, 3, 4 }
true
4new Set([1, 2, 3, 2, 1]) collapses down to just three unique values the moment it's created. has() checks membership, and size (not length) gives you the count.
Converting between Set and array
Sets don't have array methods like map() or filter() directly — convert with the spread syntax [...set] when you need those.
let unique = [...new Set([1, 1, 2, 2, 3])]; console.log(unique);
[ 1, 2, 3 ]
This is genuinely useful, not a novelty: deduplicating an array is exactly this pattern — wrap it in a Set to drop duplicates, then spread it back into a plain array.
Next: conditional statements — making your code actually branch based on a value.
Practice exercise
Create numbers = new Set([1, 2, 2, 3, 3, 3]). Print numbers.size, then print whether numbers has the value 2 using numbers.has(2).