Tricky JS Questions

✨ Test Your Knowledge ✨

What's the output?

const set = new Set([1, 1, 2, 3, 4]);

console.log(set);
  • A: `[1, 1, 2, 3, 4]`
  • B: `[1, 2, 3, 4]`
  • C: `{1, 1, 2, 3, 4}`
  • D: `{1, 2, 3, 4}`
👆Tap to reveal answer
✓ Answer

Correct Answer:

D

The Set object is a collection of unique values: a value can only occur once in a set.

We passed the iterable [1, 1, 2, 3, 4] with a duplicate value 1. Since we cannot have two of the same values in a set, one of them is removed. This results in {1, 2, 3, 4}.

👆Tap to flip back