Hello, dear coding hero! 🦸♀️🦸♂️
Today I’m going to reveal JavaScript’s hidden superpower: the Set object. It may look like an Array, but it’s a champion when it comes to unique elements only! 🏆
In this post, you’ll find basic info, practical tips, and creative examples. Let’s dive in! 💖
1️⃣ What is a Set?
Imagine you have a collection, but you want only one of each element.
That’s exactly what a Set does. It looks like an Array but doesn’t store duplicates.
const fruits = new Set();
fruits.add("apple");
fruits.add("pear");
fruits.add("apple"); // 😂 This won’t be added, Sets don’t like duplicates
console.log(fruits); // Set(2) { 'apple', 'pear' }
Explanation:
new Set()creates an empty set.add()adds values to the set.- Sets never allow duplicates, so the second “apple” doesn’t get added.
💡 Tip: If you want to remove duplicates, you can convert an Array to a Set and back to an Array. It’s a mini superpower in JS 😎.
2️⃣ Basic Set Methods: Set’s Super Weapons 🛡️
Here are the main methods you’ll use to manage a Set:
1. add(value) – The Add Magic ✨
const fruits = new Set();
fruits.add("apple");
fruits.add("pear");
fruits.add("banana");
console.log(fruits); // Set(3) { 'apple', 'pear', 'banana' }
Explanation:
add()adds a value to the Set.- If the value already exists, Set ignores it.
- Think of Set as a loyal friend: “No duplicates allowed!” 😏
💡 Tip: If you want to clean a user-submitted list, new Set(list) removes duplicates instantly.
2. delete(value) – Go Away! ✋
fruits.delete("pear");
console.log(fruits); // Set(2) { 'apple', 'banana' }
Explanation:
delete()removes the specified value from the Set.- If the value isn’t in the Set, nothing happens.
💡 Tip: delete() returns a boolean. You can check if the deletion was successful:
console.log(fruits.delete("apple")); // true
console.log(fruits.delete("pear")); // false
3. has(value) – Detective Mode 🕵️
console.log(fruits.has("apple")); // true
console.log(fruits.has("pear")); // false
Explanation:
has()checks if the value exists in the Set.- Like a secret agent: “Is this value here?” 😎
💡 Tip: Combine has() + add() to prevent users from adding duplicates:
const friends = new Set();
function addFriend(name) {
if (!friends.has(name)) {
friends.add(name);
console.log(`${name} added! 🎉`);
} else {
console.log(`${name} already exists, no duplicates! 😏`);
}
}
addFriend("Ahmet");
addFriend("Ahmet");
4. clear() – The Reset Button 🔥
fruits.clear();
console.log(fruits); // Set(0) {}
Explanation:
clear()completely empties the Set.- All values are gone, just like old memories 😂
5. size – Set’s Size 📏
const numbers = new Set([1,2,3,3,4]);
console.log(numbers.size); // 4
Explanation:
sizeshows the number of elements in the Set.- Just like
lengthin an Array.
💡 Tip: Use size to quickly limit duplicates. For example, prevent more than 5 additions.
3️⃣ Using Sets Smarter: Magical Tricks ✨
Array → Set: Remove Duplicates!
const nums = [1,2,2,3,3,3];
const uniqueNums = new Set(nums);
console.log(uniqueNums); // Set(3) { 1, 2, 3 }
Explanation:
- Duplicates in the Array are automatically removed.
💡 Tip: If your database returns messy lists, Sets can save the day!
Set → Array: Stay Flexible!
const uniqueArray = [...uniqueNums];
console.log(uniqueArray); // [1, 2, 3]
Explanation:
- Use the spread operator (
...) to convert a Set back to an Array. - This allows you to keep using Array methods.
4️⃣ Mini Game: Friends List 🥳
Imagine an app where users add friends, but you don’t want duplicates.
Set is your hero here:
const friends = new Set();
friends.add("Ahmet");
friends.add("Ayşe");
friends.add("Ahmet"); // duplicates ignored
for (let friend of friends) {
console.log(friend); // Ahmet, Ayşe
}
💖 Set is loyal: no duplicates allowed 😏
5️⃣ Bonus: Set Loops & Tips 🌀
Looping through a Set is easy:
friends.forEach(friend => console.log(`Friend: ${friend}`));
Explanation:
- Use
forEachorfor...ofto iterate over Set elements. - Unlike Maps, Sets only store values, no key-value pairs.
💡 Tip: You can also do “set math” like intersections and differences:
const setA = new Set([1,2,3,4]);
const setB = new Set([3,4,5,6]);
// Intersection
const intersection = new Set([...setA].filter(x => setB.has(x)));
console.log(intersection); // Set(2) {3, 4}
// Difference
const difference = new Set([...setA].filter(x => !setB.has(x)));
console.log(difference); // Set(2) {1, 2}
🎉 Summary
- Set = Collection without duplicates 💎
- add() = Add element
- delete() = Remove element
- has() = Check existence
- clear() = Remove all
- size = Number of elements
💡 Bonus tips:
- Array → Set = remove duplicates
- Set → Array = use Array methods
- Intersection & Difference = do set math

