JavaScript Sets — Interview Questions & Answers
Ques: What is a Set in JavaScript?
Ans: A Set is a collection of unique values, meaning it automatically removes duplicates. It can store any type of value — primitives or object references.
const mySet = new Set([1, 2, 2, 3]);
console.log(mySet); // Set(3) {1, 2, 3}
Ques: How is a Set different from an Array?
| Feature | Set | Array |
|---|---|---|
| Duplicates | Not allowed | Allowed |
| Order | Insertion order | Indexed |
| Access | No index | Indexed by number |
| Performance | Faster for lookups | Slower for large datasets |
Ques: How do you create a Set?
Ans: Using the Set() constructor:
const s1 = new Set();
const s2 = new Set([10, 20, 30]);
Ques: How do you add elements to a Set?
Ans: Use the .add() method:
const fruits = new Set();
fruits.add("apple");
fruits.add("banana");
Ques: How do you check if an element exists in a Set?
Ans: Use .has() method:
fruits.has("apple"); // true
Ques: How do you remove elements from a Set?
Ans: Use .delete() or .clear():
fruits.delete("apple");
fruits.clear(); // removes all
Ques: How do you find the size of a Set?
Ans: Use the .size property:
console.log(fruits.size);
Ques: How can you iterate over a Set?
Ans: You can use for...of, forEach, or convert it into an array.
for (let item of mySet) console.log(item);
Ques: How do you convert a Set to an Array?
Ans: Using the spread operator or Array.from():
const arr = [...mySet];
Ques: How do you remove duplicates from an Array using Set?
const nums = [1, 2, 2, 3];
const unique = [...new Set(nums)];
console.log(unique); // [1, 2, 3]
Ques: What are Set operations like Union, Intersection, and Difference?
Ans: You can perform these using spread and filter:
Union:
const union = new Set([...setA, ...setB]);
Intersection:
const intersection = new Set([...setA].filter(x => setB.has(x)));
Difference:
const difference = new Set([...setA].filter(x => !setB.has(x)));
Ques: Are Sets iterable?
Ans: Yes, You can loop through them with for...of or spread them.
Ques: Can a Set contain NaN or undefined?
Ans: Yes, Both are valid unique values in a Set.
new Set([NaN, NaN, undefined]).size; // 2
Ques: Can objects be stored in a Set?
Ans: Yes — but remember, each object has a unique reference.
const set = new Set();
set.add({a:1});
set.add({a:1});
console.log(set.size); // 2 (different references)
Ques: What is the WeakSet in JavaScript?
Ans: A WeakSet is similar to Set but stores only objects, not primitives.
It also holds weak references, meaning objects can be garbage-collected.
let obj = {name: "JS"};
const ws = new WeakSet();
ws.add(obj);
obj = null; // Object can be garbage collected
Ques: Difference between Set and WeakSet
| Feature | Set | WeakSet |
|---|---|---|
| Data types | Any type | Only objects |
| Iteration | Iterable | Not iterable |
| Garbage collection | No | Yes (weak references) |
| Size property | Yes | No |
Ques: Can WeakSets be iterated or converted to arrays?
Ans: No, because their contents are not enumerable — they exist for memory efficiency, not data iteration.
Ques: What are practical use cases of Sets?
- Removing duplicates
- Fast existence checks (
has()) - Tracking unique user IDs or values
- Performing mathematical set operations
