Lesson 27 of 48
56%
JavaScript Sets
A Set is a collection of unique values.
If you try to add the same value twice, it still appears once.
Think of a sticker album that refuses duplicate stickers.
1
Make a Set
2
Add and delete
3
Check with has
4
Size
5
Loop through a Set
Create a Set
Use new Set().
You can start empty or pass an array.
javascript
add
add puts a value in if it is new.
javascript
has and delete
has asks if a value is there.
delete removes it.
javascript
size
.size tells how many unique values you have.
javascript
Loop
You can walk through each value with for...of.
javascript
Array to Set to Array
Sets are great for removing duplicates from a list.
javascript
Tip: Reach for a Set when uniqueness matters more than order labels.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.