JavaScript ES2024 ·
✓ verified by execution on 2026-08-15
A Set is a collection of values where each value may only occur once. If you have a list of items and you need to ensure there are no duplicates, a Set is the perfect tool for the job.
Creating and Adding to a Set
You can create an empty Set and add values to it using the .add() method. Notice what happens when we try to add the same number twice:
javascript
const s = new Set();s.add(1);s.add(1);console.log(s.size);
Output
1
Your output
The second .add(1) did nothing. A value can only exist once in a Set.
Sets can store values of any type, and they strictly compare them. For instance, the number 1 is different from the string '1':
javascript
const s = new Set();s.add(1);s.add('1');console.log(s.size);
Output
2
Your output
Removing Duplicates from an Array
The most common use case for a Set is removing duplicate elements from an array. You can pass an array directly into the new Set() constructor, which instantly filters out duplicates. Then, you can spread ... the Set back into a new array:
To check if a Set contains a specific value, use the .has() method. It returns true or false:
javascript
const s = new Set([1, 2, 3]);console.log(s.has(2));
Output
true
Your output
To remove a value, use the .delete() method:
javascript
const s = new Set([1, 2, 3]);s.delete(2);console.log(s.has(2));
Output
false
Your output
Common Misconceptions
1. Using .length instead of .size
Arrays use .length to count their items, but Sets use .size. If you try to check .length on a Set, you will get undefined instead of an error, which can cause silent bugs.
2. The Object Reference Trap
It is easy to assume that two objects with identical contents are the same. But object equality in JavaScript is based on reference, not structure.
Predict the outputjavascript
Read the code. What exactly will it print? Commit to an answer before you look.
const s = new Set([{}, {}]);console.log(s.size);
Output
2
You predicted
Because {} creates a brand new, distinct object every time, the two empty objects are unique references. The Set happily stores both of them.
To visualize how values are added and checked in a Set over time, let’s look at a step-by-step trace:
javascript · visualize
const mySet = new Set();mySet.add('apple');mySet.add('banana');mySet.add('apple');const hasBanana = mySet.has('banana');
A Set holds distinct values, unlike an Array which holds values at specific indexes.
Edge Cases
The Case of NaN
In JavaScript, NaN (Not-a-Number) normally does not equal itself (NaN === NaN is false). However, Sets use a special equality check (SameValueZero) which specifically equates NaN with NaN. This means you can only add NaN to a Set once:
javascript
const s = new Set();s.add(NaN);s.add(NaN);console.log(s.size);
Output
1
Your output
Positive and Negative Zero
Similarly, +0 and -0 are considered the same value in a Set. If you add both, the Set will only store the first one.
Check yourself
Can a JavaScript Set contain multiple identical values?
Reveal answer
No, every value in a Set must be unique. — A Set is a collection of values where each value may only occur once. If you try to add a value that already exists, the Set simply ignores it.
How do you check if a Set named mySet contains the number 5?
Reveal answer
mySet.has(5) — Sets use the .has() method to check for a value. Arrays use .includes(), but Sets use .has().
What is the correct way to find out how many items are in mySet?
Reveal answer
mySet.size — Unlike arrays which use .length, Sets use the .size property.
How many items will be in a Set if you add NaN twice?
Reveal answer
One, because Sets treat NaN as equal to NaN. — Sets use the SameValueZero algorithm for equality, which specifically equates NaN with NaN. Therefore, a Set can only hold one NaN.
Challenges
Challenge 1 +10 XP
Given an array of strings representing tags, use a Set to return a new array with all duplicate tags removed.
javascript
Test 1 — expects "js,web,html"
Need a hint? (−25% XP)
Pass the array to `new Set()`, then spread it back into an array using `[...]`.