SKU // 73

JavaScript Set Logic

Sets come from math, and math has a standard set of ways to combine and compare them: which items are in both groups, which are in either, which are in one but not the other. JavaScript has 7 built-in methods for them. They reached browsers in 2024 and became an official part of the language in its 2025 edition.

For every example in this lesson:

const soccer = new Set(["Ava", "Ben", "Cal", "Dee"]);
const band   = new Set(["Cal", "Dee", "Eli"]);

// union(): In Either

Everyone in soccer or band (or both), each listed once:

console.log([...soccer.union(band)]);
["Ava", "Ben", "Cal", "Dee", "Eli"]

// intersection(): In Both

Students who do soccer and band:

console.log([...soccer.intersection(band)]);
["Cal", "Dee"]

// difference(): In the First, Not the Second

Soccer players who are not in band. Order matters here:

console.log([...soccer.difference(band)]);   // soccer only
console.log([...band.difference(soccer)]);   // band only
["Ava", "Ben"] ["Eli"]

// symmetricDifference(): In Exactly One

Students in one activity but not both:

console.log([...soccer.symmetricDifference(band)]);
["Ava", "Ben", "Eli"]
Picture It
Imagine two overlapping circles (a Venn diagram). union is everything shaded. intersection is just the overlap. difference is one circle with the overlap cut out. symmetricDifference is both circles with the overlap cut out.

// Comparison Methods (Return true or false)

const starters = new Set(["Ava", "Cal"]);
const chess    = new Set(["Fay", "Gus"]);

starters.isSubsetOf(soccer)      // true   every starter is on the soccer team
soccer.isSupersetOf(starters)    // true   soccer contains all the starters
soccer.isDisjointFrom(chess)     // true   no one in common
soccer.isDisjointFrom(band)      // false  Cal and Dee are in both
true true true false

// The Argument Must Be a Set

These methods need a Set (or a Map, whose keys are used) as the argument. An array does not work, even though new Set() happily accepts one:

const a = new Set([1, 2]);

console.log([...a.union(new Set([3]))]);   // works
console.log([...a.union([3])]);          // an array
[1, 2, 3] TypeError: The .size property is NaN

The error mentions .size because the method looks for a size, and arrays have length instead. Wrap the array: a.union(new Set([3])).

// Summary

Method Question Returns
a.union(b)In a or b?new Set
a.intersection(b)In both?new Set
a.difference(b)In a but not b?new Set
a.symmetricDifference(b)In exactly one?new Set
a.isSubsetOf(b)Is all of a inside b?boolean
a.isSupersetOf(b)Is all of b inside a?boolean
a.isDisjointFrom(b)Nothing in common?boolean
Good to Know
None of these methods change the original Sets. The first four return a brand-new Set, so you can chain them: a.union(b).difference(c).

// For Older Browsers

These methods arrived in 2024. Browsers from before then do not have them. The same results can be built with spread and filter, which is also a good way to understand what each one does:

const union        = new Set([...soccer, ...band]);
const intersection = new Set([...soccer].filter(x => band.has(x)));
const difference   = new Set([...soccer].filter(x => !band.has(x)));

// Real Example: What Changed?

const yesterday = new Set(["ava", "ben", "cal"]);
const today     = new Set(["ben", "cal", "dee"]);

console.log("Joined:", [...today.difference(yesterday)]);
console.log("Left:",   [...yesterday.difference(today)]);
console.log("Stayed:", [...today.intersection(yesterday)]);
Joined: ["dee"] Left: ["ava"] Stayed: ["ben", "cal"]

// Quick Check

A = new Set([1, 2, 3]) and B = new Set([2, 3, 4]). What is in A.difference(B)?

  1. 1
  2. 4
  3. 1 and 4
  4. 2 and 3
Show answer
A. difference keeps what is in A but not in B. C would be symmetricDifference, and D would be intersection.

What happens?

const s = new Set(["a"]).union(["b"]);
Show answer
TypeError: The .size property is NaN. The argument must be a Set (or Map). Use new Set(["a"]).union(new Set(["b"])).

Which of these returns true or false instead of a new Set?

  1. intersection
  2. union
  3. isSubsetOf
  4. symmetricDifference
Show answer
C. The three is... methods compare Sets and answer yes or no. The other four build a new Set.

// Try It

  1. Click Open Playground. Put your favorite foods and a friend's in the two Sets, then find the foods you both like
  2. Find foods only you like, and foods only your friend likes
  3. Check whether new Set("aeiou") is disjoint from new Set("rhythm")
  4. Rebuild symmetricDifference using only spread, filter, and has
// Video Walkthroughs
Last updated: Oct 10, 2026, 1:42 AM ET