Set?A Set in JavaScript is a built-in object that lets you store unique values of any type.
Unlike arrays, a Set does not allow duplicate entries — each value appears only once.
Think of it like a real-world set of items: no duplicates, no ordering rules, just pure uniqueness.
const mySet = new Set([1, 2, 3, 3]);
console.log(mySet);
// Output: Set(3) { 1, 2, 3 } Even though 3 appears twice, the Set keeps only one instance.
Achieve your career goals, switch to tech and learn software development in Abuja
Using a Set gives you useful advantages:
Perfect when you need distinct values (e.g., unique users, tags, IDs).
Checking if a value exists is O(1) in a Set, while arrays take O(n).
One line of code!
const uniqueArray = [...new Set([1,1,2,3,3])];
// [1, 2, 3] Union, intersection, difference — very easy to implement.
const s = new Set(); const s = new Set([1, 2, 3]); .add(value)Adds a value to the Set.
const s = new Set();
s.add(10);
s.add(20);
s.add(10); // ignored because 10 already exists .delete(value)Removes a value if it exists.
s.delete(20); .has(value)Checks if a value exists inside the Set.
console.log(s.has(10)); // true
console.log(s.has(99)); // false .clear()Removes all items.
s.clear();
console.log(s.size); // 0 .sizeProperty that shows the number of unique elements.
console.log(s.size); Sets are iterable. You can loop through them in different ways:
for...ofconst fruits = new Set(["apple", "banana", "orange"]);
for (let fruit of fruits) {
console.log(fruit);
} .forEach()fruits.forEach(f => console.log(f)); Useful for indexing or using array methods.
const arr = [...fruits];
console.log(arr); const students = ["Ada", "Ben", "Ben", "Lara"];
const uniqueStudents = [...new Set(students)];
console.log(uniqueStudents);
// ["Ada", "Ben", "Lara"] const visitors = new Set();
visitors.add("user1");
visitors.add("user2");
visitors.add("user1"); // ignored const tags = ["js", "tech", "js", "web"];
const uniqueTags = [...new Set(tags)]; Combine items from both sets.
const A = new Set([1,2]);
const B = new Set([2,3]);
const union = new Set([...A, ...B]);
// Set {1, 2, 3} Values that appear in both sets.
const intersection = new Set([...A].filter(x => B.has(x)));
// Set {2} Values in A that are not in B.
const difference = new Set([...A].filter(x => !B.has(x)));
// Set {1} This will not work:
console.log(s[0]); // undefined Even though they don’t support indexing, they remember the order in which items were added.
This is different from regular equality in JavaScript.
new Set([NaN, NaN]); // Set { NaN } | Feature | Set | Array |
|---|---|---|
| Allows duplicates | ❌ No | ✅ Yes |
| Indexed access | ❌ No | ✅ Yes |
| Fast existence check | ✅ Yes | ❌ No |
| Useful for uniqueness | ✅ Excellent | ❌ Requires work |
| Order preserved | ✔️ Yes | ✔️ Yes |
add, delete, has, clear.Latest tech news and coding tips.
What is Steam Locomotive (sl)? Steam Locomotive (sl) is a small terminal program on Unix/Linux systems…
What is Rate Limiting? Download this article as a PDF on the Codeflare Mobile App…
Learn on the Go. Download the Codeflare Mobile from iOS App Store. 1. What is…
Download the Codeflare iOS app and learn on the Go 1. What UI and UX…
1. Running Everything as Root One of the biggest beginner errors. Many new users log…
A keylogger is a type of surveillance software or hardware that records every keystroke made…