Skip to solution
easyFrontend

How do the new Set methods `union()`, `intersection()`, `difference()` and `symmetricDifference()` work?

435 views
01

Understand the problem

Question presented to candidate: "Before these Set methods existed, you'd write manual loops (or filter/some combinations) to find the overlap between two Sets. What do union, intersection, difference, and symmetricDifference actually give you — and do any of them mutate the original Sets?"

What a strong answer should cover:

  • 📌 Interview term: Set.prototype.union(other) — returns a NEW Set containing every element from BOTH sets, real set-theory union — verified directly with two overlapping real Sets.
  • 📌 Interview term: Set.prototype.intersection(other) — returns a new Set containing only elements present in BOTH sets.
  • 📌 Interview term: Set.prototype.difference(other) — returns a new Set with elements in the FIRST set but NOT the second — verified directly to be genuinely asymmetric: a.difference(b) and b.difference(a) produce real, different results.
  • 📌 Interview term: Set.prototype.symmetricDifference(other) — returns a new Set with elements in EITHER set but not both — verified directly.
  • 📌 Interview term: the real, direct answer to the prompt's mutation question — verified directly: none of these methods mutate the original Set at all — calling .union() genuinely left the original Set's .size completely unchanged, matching the identical non-mutating pattern this bank's own array-methods coverage establishes for map/filter/slice.
  • A precise answer names that these methods genuinely work on any "Set-like" object (anything with a real .size, .has(), and .keys()), not strictly a real Set instance — verified directly — plus the three real boolean-returning bonus methods that shipped alongside them: isSubsetOf, isSupersetOf, isDisjointFrom.

Clarifying questions expected:

  • None — this is a definitional/technical question; directly answering the prompt's own mutation question (none of them mutate) is the strong signal.

Code / implementation expected: Yes — real results for all four operations on the same two overlapping Sets, plus the direct non-mutation proof, is the clearest demonstration.

setes2024collection
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript fundamentals interviews. Difficulty: Medium

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every operation's real result below was actually run in Node (Baseline-shipped across all

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSReal results for union/intersection/difference/symmetricDifference on two overlapping Sets, plus direct proof none of them mutate the original
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 27 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track