JavaScript Set Interview Questions and Answers
Understand Set membership, uniqueness, iteration, and when a Set is a better fit than an array.
Frequently Asked Questions
Important questions are highlighted in the sections below; select one to jump to its answer.
- Convert a Set into an array while keeping unique values.
- How can a `Set` remove duplicate numbers from an array?
- How to set default values in object destructuring?
- How to print Set values using for...of loop?
- What does a `Set` guarantee about its values?
- How do you check whether a value exists in a `Set`?
- Why does a `Set` keep two objects with identical properties?
Theoretical JavaScript Interview Questions
Add = defaultValue after the variable name in your pattern: const [a = 0, b = 0] = arr or const { name = 'anonymous' } = user. The default only kicks in when the value is undefined - not null, not 0, not empty string. This is really handy when working with optional config objects or partially-defined API responses where some fields might be missing.
Assign defaults inline: const { role = 'user', limit = 10 } = config. If the property is missing or undefined, the default kicks in - but not for null. This is the standard pattern for optional config objects: function init({ timeout = 3000, retries = 3 } = {}) {}. The = {} at the end means the function still works if called with no arguments at all.
A Set is a collection of unique values - it automatically removes duplicates, which makes it perfect for deduplicating arrays: [...new Set(arr)]. Unlike an array, a Set doesn't have indexed access, but iteration order is guaranteed to be insertion order. Sets use strict equality (===) for comparison, so two different objects that look identical are still considered unique.
WeakSet is like Set but only holds objects, and it holds them weakly - meaning if nothing else references an object, the garbage collector can clean it up even if it's in the WeakSet. You can't iterate over it or check its size, which limits its use cases. The main practical use is tracking whether you've already processed an object without creating a memory leak.
Since Set is iterable, you can loop over it directly: for (const value of mySet) { console.log(value); }. You can also call mySet.forEach(v => console.log(v)) or explicitly use mySet.values(). All approaches iterate in insertion order, which is a nice guarantee compared to iterating plain object keys.
A `Set` stores unique values according to `SameValueZero` equality. Adding a value already present does not create another entry.
Use `set.has(value)`, which returns a boolean indicating whether that value is present. It expresses membership directly without searching an array manually.
A `Set` iterates values in insertion order. Deleting and then re-adding a value places it at the end of the iteration order.
`Set` uses `SameValueZero`: `NaN` is considered equal to itself and positive and negative zero are considered the same. Objects are unique by reference identity, not by their property contents.
The objects are distinct references, so `Set` treats them as different values even when their properties match. To deduplicate records by content, derive a stable key or implement an explicit comparison strategy.
A `WeakSet` is useful when object membership should not by itself keep those objects alive, because its object references are weak. It is not iterable and cannot expose a size, so use a regular `Set` when enumeration is needed.
JavaScript Practical and Coding Interview Questions
Problem: Convert a Set containing repeated input values into an array and log the unique values.
const tags = new Set(['js', 'web', 'js']);
const uniqueTags = Array.from(tags);
console.log(uniqueTags);Problem: Use a `Set` to remove duplicate numbers while preserving their first-seen order, then log the resulting array.
const numbers = [4, 2, 4, 1, 2, 3];
const uniqueNumbers = [...new Set(numbers)];
console.log(uniqueNumbers);Problem: Create a new `Set` containing only values shared by both input sets, without modifying either input. Log the intersection.
const first = new Set([1, 2, 3, 5]);
const second = new Set([2, 4, 5]);
const intersection = new Set(
[...first].filter((value) => second.has(value))
);
console.log([...intersection]);Problem: Combine the values from both input sets into a new set, preserving insertion order and leaving the original sets unchanged.
const first = new Set(["red", "blue"]);
const second = new Set(["blue", "green"]);
const union = new Set([...first, ...second]);
console.log([...union]);