Exercise
You can use a for...of loop to iterate over every value in a Set. The loop visits each element in the order it was inserted. Since Sets only store unique values, every iteration yields a distinct element.
const colors = new Set(["red", "green", "blue"]);
for (const color of colors) {
console.log(color);
}
// Output:
// "red"
// "green"
// "blue"
The forEach() method executes a callback for each value in the Set. Unlike Arrays, the callback receives (value, value, set) - the first and second arguments are both the value because Sets have no keys. This keeps the signature consistent with Map.forEach().
const fruits = new Set(["apple", "banana", "cherry"]);
fruits.forEach((value, valueAgain, set) => {
console.log(value, valueAgain, value === valueAgain);
});
// Output:
// "apple" "apple" true
// "banana" "banana" true
// "cherry" "cherry" true
There are two common ways to convert a Set to an Array. You can use the spread operator [...set] or the Array.from() method. Both approaches produce a new Array containing all values from the Set in insertion order.
const mySet = new Set([10, 20, 30]);
// Method 1 - Spread operator
const arr1 = [...mySet];
console.log(arr1); // [10, 20, 30]
// Method 2 - Array.from()
const arr2 = Array.from(mySet);
console.log(arr2); // [10, 20, 30]
You can convert an Array to a Set by passing the array directly to the Set constructor. The constructor accepts any iterable, so it reads each element from the array and adds it to the Set. Duplicate values in the array are automatically removed.
const numbers = [1, 2, 3, 2, 1, 4];
const numberSet = new Set(numbers);
console.log(numberSet); // Set {1, 2, 3, 4}
console.log(numberSet.size); // 4
The most common pattern for removing duplicates is [...new Set(array)]. This creates a Set from the array (which discards duplicates) and then spreads it back into a new array. It is a concise one-liner that works with primitive values.
const items = ["a", "b", "a", "c", "b", "d"];
const unique = [...new Set(items)];
console.log(unique); // ["a", "b", "c", "d"]
// Also works with numbers
const nums = [5, 3, 5, 7, 3, 9];
const uniqueNums = [...new Set(nums)];
console.log(uniqueNums); // [5, 3, 7, 9]
A Set provides three iterator methods. The values() method returns an iterator of all values. The keys() method is an alias for values() - they return the same iterator because Sets do not have separate keys. The entries() method returns an iterator of [value, value] pairs to stay consistent with the Map interface.
const mySet = new Set(["x", "y", "z"]);
// values()
for (const val of mySet.values()) {
console.log(val); // "x", "y", "z"
}
// keys() - produces the same output as values()
for (const key of mySet.keys()) {
console.log(key); // "x", "y", "z"
}
// entries() - returns [value, value] pairs
for (const entry of mySet.entries()) {
console.log(entry); // ["x","x"], ["y","y"], ["z","z"]
}
The union of two Sets contains all unique elements from both Sets. You can create a union by spreading both Sets into a new Set. Since the Set constructor automatically removes duplicates, any shared elements appear only once in the result.
const setA = new Set([1, 2, 3]);
const setB = new Set([3, 4, 5]);
const union = new Set([...setA, ...setB]);
console.log(union); // Set {1, 2, 3, 4, 5}
The intersection of two Sets contains only the elements that exist in both Sets. You can find the intersection by converting one Set to an array, filtering it to keep only values present in the other Set, and then creating a new Set from the filtered result.
const setA = new Set([1, 2, 3, 4]);
const setB = new Set([3, 4, 5, 6]);
const intersection = new Set(
[...setA].filter(value => setB.has(value))
);
console.log(intersection); // Set {3, 4}
The difference of Set A minus Set B contains elements that are in Set A but not in Set B. You can compute this by spreading Set A into an array, filtering out any values that exist in Set B, and wrapping the result in a new Set.
const setA = new Set([1, 2, 3, 4]);
const setB = new Set([3, 4, 5, 6]);
const difference = new Set(
[...setA].filter(value => !setB.has(value))
);
console.log(difference); // Set {1, 2}
A Set is a subset of another Set if every element in the first Set also exists in the second Set. You can check this by converting the smaller Set to an array and using the every() method to verify that each value is present in the larger Set.
const setA = new Set([1, 2]);
const setB = new Set([1, 2, 3, 4, 5]);
function isSubset(subset, superset) {
return [...subset].every(value => superset.has(value));
}
console.log(isSubset(setA, setB)); // true
console.log(isSubset(setB, setA)); // false