JavaScript Sets
A Set is a collection of unique values. Each value can only occur once in a Set. Sets can hold any value of any type, including primitives and object references. They are ideal for removing duplicates and checking membership.
Creating a Set
You create a Set with the new Set() constructor. You can pass an iterable (like an array) to initialize it with values. Duplicate values are automatically removed.
// Empty Set
const set1 = new Set();
console.log("Empty:", set1);
// Set from array (duplicates removed)
const set2 = new Set([1, 2, 3, 2, 1, 4]);
console.log("From array:", set2);
console.log("Size:", set2.size);
// Set from string (each character)
const set3 = new Set("hello");
console.log("From string:", set3);add(), has(), and delete()
add() inserts a new element. has() checks if a value exists (returns boolean). delete() removes a value and returns true if it existed. These operations are very fast -- O(1) on average.
const colors = new Set();
// add() - returns the Set (chainable)
colors.add("Red").add("Green").add("Blue");
console.log("Set:", colors);
// Adding duplicate - silently ignored
colors.add("Red");
console.log("After duplicate add:", colors);
console.log("Size:", colors.size);
// has() - check membership
console.log("Has Red:", colors.has("Red"));
console.log("Has Yellow:", colors.has("Yellow"));
// delete() - remove element
const deleted = colors.delete("Green");
console.log("Deleted Green:", deleted);
console.log("After delete:", colors);
// clear() - remove all elements
colors.clear();
console.log("After clear:", colors.size);Iterating Sets
Sets maintain insertion order and can be iterated with for...of, forEach(), or by converting to an array with the spread operator.
const fruits = new Set(["Apple", "Banana", "Cherry", "Date"]);
// for...of
for (const fruit of fruits) {
console.log("for...of:", fruit);
}
// forEach
fruits.forEach((value) => {
console.log("forEach:", value);
});
// keys(), values(), entries()
console.log("keys:", [...fruits.keys()]);
console.log("values:", [...fruits.values()]);
console.log("entries:", [...fruits.entries()]);Set from Array and Removing Duplicates
One of the most common uses of Set is removing duplicate values from an array. Convert the array to a Set and back to an array.
const numbers = [1, 2, 3, 2, 4, 1, 5, 3, 6];
// Remove duplicates with Set
const unique = [...new Set(numbers)];
console.log("Unique:", unique);
// Also works with Array.from()
const unique2 = Array.from(new Set(numbers));
console.log("Unique2:", unique2);
// Remove duplicate strings (case-sensitive)
const words = ["hello", "Hello", "world", "hello", "WORLD"];
const uniqueWords = [...new Set(words)];
console.log("Unique words:", uniqueWords);
// Case-insensitive dedup
const lowerSet = new Set(words.map(w => w.toLowerCase()));
console.log("Case-insensitive:", [...lowerSet]);Set vs Array
Sets and Arrays serve different purposes. Use a Set when you need unique values and fast lookups. Use an Array when you need ordered data with index access.
| Feature | Set | Array |
|---|---|---|
| Duplicates | Not allowed | Allowed |
| Order | Insertion order | Index-based order |
| Access by index | Not supported | arr[i] |
| has/includes | O(1) | O(n) |
| Size | .size property | .length property |
| Use case | Unique values, membership | Ordered collections |