(No repetition, no drama 😌)
Hello, beautiful human 💖
Today, we’re meeting an elite data structure in the JavaScript universe that politely but firmly says,
“Please, no duplicates”: Set.
Set is JavaScript’s:
- “I like things simple” type
- “Duplicates? Not my thing” kind of guardian
- Quiet, but very powerful character
🤯 If This Feels Familiar to You…
If you’ve ever:
- Thought “I swear this element was already in this array…” 🤔
- Bounced endlessly between
filter,indexOf, andincludes - Said “My hair is turning gray because of duplicate values” 👵🏻
👉 Welcome. Set is made for you.
🧺 What Is a Set? (The Simplest Explanation)
A Set is a collection in JavaScript that stores unique values.
The rule is crystal clear:
“Two of the same value? Absolutely not.”
const mySet = new Set();
That’s it.
- No
key - No
valueconfusion - No JSON drama
Just values and elegance ✨
🚫 Duplicate Values? Set Says: “Not Allowed.”
Let’s see Set’s personality in action:
const numbers = new Set([1, 2, 2, 3, 3, 3]);
console.log(numbers);
// Set(3) {1, 2, 3}
What happened here?
2tried to come in a second time → rejected at the door3showed up for the third time → blocked immediately
Let’s compare:
- Array: “I’ll take everything, that’s your problem.”
- Set: “Duplicates stay outside.” 🚷
💡 Practical Tip:
If an array contains duplicate values and you want to clean it up in one single line, Set is your best friend.
➕ Adding Elements to a Set (Polite but Firm)
const fruits = new Set();
fruits.add("🍎");
fruits.add("🍌");
fruits.add("🍎");
console.log(fruits);
// Set(2) {"🍎", "🍌"}
What happened?
- 🍎 first time → welcome
- 🍌 arrives → come on in
- 🍎 again → “We’ve already met.”
Set isn’t rude, but it is decisive 😌
💡 Practical Tip:
The add() method is chainable:
fruits.add("🍓").add("🍍");
❓ Does It Exist or Not? (The Fastest Check)
fruits.has("🍌"); // true
fruits.has("🍓"); // false
The beauty here:
- No
includes - No
indexOf - No performance worries
Just:
has → does it exist?
Clean. Confident. 💅
💡 When Is This Useful?
- Permission checks
- Favorites lists
- Checking if something was already processed
🗑️ Removing Elements & Cleaning Up
fruits.delete("🍌");
For one-by-one breakups.
fruits.clear();
Mass goodbye.
The house is clean, the Set is spotless 🧹
💡 Practical Tip:delete() returns true or false:
fruits.delete("🍎"); // true
fruits.delete("🍉"); // false
📏 Size of a Set (Pay Attention!)
fruits.size;
No length ❌
Because Set says:
“I’m not an array, darling.”
💡 Interview Warning 🚨
If you write mySet.length, you’ll get undefined.
This tiny mistake catches a lot of people 👀
🔄 Iterating Over a Set (Calm and Peaceful)
for (const fruit of fruits) {
console.log(fruit);
}
Or:
fruits.forEach(fruit => console.log(fruit));
Set is:
- Ordered (by insertion order)
- Quiet
- Drama-free
🧘♀️ Zen mode: ON.
🔁 Array ↔ Set Conversion (The Legendary Part)
Removing duplicates from an array:
const numbers = [1, 1, 2, 3, 3, 4];
const uniqueNumbers = [...new Set(numbers)];
console.log(uniqueNumbers);
// [1, 2, 3, 4]
This single line:
- Saves interviews
- Cleans bugs
- Makes people ask, “How did you do that?” 😌
💡 Golden Tip:
To convert a Set back into an array:
Array.from(new Set(numbers));
🎯 When Should You Use a Set?
✔️ If you don’t want duplicate values
✔️ If you need fast “exists or not?” checks
✔️ If you want to clean an array
✔️ If you want cleaner, more readable code
Set is there…
Waiting quietly for you 🖤
💌 Final Words
Set is JavaScript’s data structure that:
- Hates drama
- Loves simplicity
- Lives by the rule: “Less, but better”
If your code has repeating things…
Maybe, just like life…
👉 You need a Set 😏

