🧠 Understanding JavaScript Set: A Cleaner Way to Store Unique Data
Imagine you’re running a party RSVP list. People keep texting you their names—some twice, some even more! You don’t want to manually weed out duplicates. You just want a clean list of unique attendees.
Enter JavaScript’s Set
, your automated guest bouncer! 👮♂️
A Set
is a collection of values where each value must be unique. That means no duplicates allowed!
Let’s see how this works in practice.
🧪 Creating a Set
You create a new set like this:
let mySet = new Set();
Or you can initialize it with values:
let colors = new Set(["red", "blue", "green"]);
Duplicate values? No problem—they’ll be ignored:
let dupSet = new Set(["a", "b", "a", "c"]);
console.log(dupSet); // Set(3) { "a", "b", "c" }
🎯 Core Features of a Set
A JavaScript Set has the following features:
- Uniqueness: No two values can be the same.
- Order: The values are kept in the order of insertion.
- Value Types: Any type of value can be stored—numbers, strings, objects, even other sets!
🔧 Common Set Methods
Let’s explore the tools in your Set toolbox.
✅ add(value)
Adds a value to the Set:
colors.add("yellow");
Even if you try adding “yellow” again, it won’t be duplicated.
❌ delete(value)
Removes the value if it exists:
colors.delete("blue");
❓ has(value)
Checks if a value exists:
console.log(colors.has("green")); // true
🧹 clear()
Removes all elements:
colors.clear();
📏 size
Gives the number of items in the Set:
console.log(colors.size); // 0
🔁 Iterating through a Set
You can use for...of
:
for (let color of colors) {
console.log(color);
}
Or go full pro with forEach
:
colors.forEach(function(value) {
console.log(value);
});
💡 Use Cases for Sets
- Removing duplicates from an array:
let nums = [1, 2, 3, 2, 4, 1];
let uniqueNums = [...new Set(nums)];
console.log(uniqueNums); // [1, 2, 3, 4]
- Checking existence without searching a whole array:
let users = new Set(["alice", "bob"]);
console.log(users.has("alice")); // true
- Storing flags or tags where order doesn’t matter but uniqueness does.
🧬 Set vs. Array: When Should You Use Set?
Feature | Array | Set |
---|---|---|
Allows duplicates | ✅ Yes | ❌ No |
Easy to search (.includes() ) |
✅ Yes | ✅ Yes (with .has() ) |
Iteration | ✅ Yes | ✅ Yes |
Order preserved | ✅ Yes | ✅ Yes (insertion order) |
Performance (for large sets) | Slower for lookups | Faster for lookups |
If your main goal is to store unique values and you don’t care about indices—Set
is the clear winner.
📌 Summary
JavaScript’s Set
is your go-to structure for handling unique collections. It’s lightweight, elegant, and makes your code cleaner—especially when dealing with filtering duplicates or storing non-repeating data like usernames, tags, or IDs.
✅ Review and Practice Questions
- What’s the main difference between a Set and an Array in JavaScript?
-
What will this code log?
let s = new Set(["x", "y", "x", "z"]); console.log(s.size);
- Write a function that removes duplicate items from an array using a Set.
- How do you check if a Set contains a specific value?
-
What is the output of this code?
let ids = new Set(); ids.add(1); ids.add(1); ids.add("1"); console.log(ids);