Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Combining Sets With union() and intersection()

Combining Sets With union() and intersection()

Every plot holder at the garden should come to one of the two work days. In this lesson, you ported the lines of Rosa’s script that check, from Python’s set operators to JavaScript’s Set methods.

Here are the lines. The plot records keep only each plot’s number and holder:

Language: Python
plots = [
    {"plot": 1, "holder": "Ana Lima"},
    {"plot": 2, "holder": "Ben Carter"},
    {"plot": 3, "holder": "Chloe Park"},
    {"plot": 4, "holder": "Dara Quinn"},
    {"plot": 5, "holder": "Eli Ross"},
    {"plot": 6, "holder": "Fatima Sheikh"},
    {"plot": 7, "holder": "Gus Tanaka"},
    {"plot": 8, "holder": "Hana Uddin"},
    {"plot": 9, "holder": "Ivan Volkov"},
]
spring = {
    "Ana Lima", "Ben Carter", "Chloe Park",
    "Dara Quinn", "Gus Tanaka", "Hana Uddin",
}
autumn = {
    "Ben Carter", "Eli Ross", "Gus Tanaka", "Ivan Volkov",
}
holders = {p["holder"] for p in plots}

helped = spring | autumn
both = spring & autumn
missed = holders - helped

thanks = ", ".join(sorted(both))
print(f"Thanks for coming to both: {thanks}")
reminder = ", ".join(sorted(missed))
print(f"Work day reminder: {reminder}")
  • spring | autumn is everyone who helped, spring & autumn is the people who came to both, and holders - helped is who missed both. Python thanks Ben Carter and Gus Tanaka, and reminds Fatima Sheikh.
  • Ported line by line, each set display becomes new Set() around an array. JavaScript has no set comprehension, so a for...of loop over the plots adds each holder. The three operator lines are copied unchanged, with const in front.
  • Logged before the report lines are ported, helped and both are 0 and 0, missed is NaN, and helped.size is undefined. There’s no error and no warning: every line ran, and none of them made a Set.
  • In both languages, | and & between two whole numbers are bitwise operators. Python’s set type gives them a second meaning, but JavaScript has no way for a type to do that, so they only work on numbers.
  • Each Set turns into text, "[object Set]", which isn’t a number, so it’s NaN. Bitwise operators count NaN as 0, and 0 | 0 is 0. - subtracts, and a Set minus 0 is NaN. helped is then the number 0, and a number has no size, so helped.size is undefined.
  • JavaScript names each set operation as a method instead: spring.union(autumn) for the bar and spring.intersection(autumn) for the ampersand. helped is a new Set of all eight names, spring’s first, then autumn’s two new ones, and both holds Ben Carter and Gus Tanaka. Each method leaves spring and autumn alone, as Python’s operators do. These methods are recent additions to JavaScript, and Node 26 has them all.
  • For the minus, holders.difference(helped) keeps the holders who aren’t in helped: one name, Fatima Sheikh.
  • Watch out for lists: Python’s union() takes a list too, but these methods want a Set. Given an array, they stop with a TypeError about its size, so wrap the array in new Set() first.
  • A JavaScript Set keeps insertion order, while Python’s sets promise no order: printed as it is, both can come out either way from one run to the next. So Python’s script sorts before it joins, and the port sorts too, since insertion order follows the lists, not the alphabet. A Set has no toSorted(), so spread it into an array, sort the copy, and join it with commas.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:02 With the harvest show report done, Rosa’s script turns to the work days. Every plot holder should come to one of the two. In this lesson, you’ll port the lines that check.

00:14 The script keeps each day’s names in a set, spring and autumn, and the nine plot holders in a third. A vertical bar gives everyone who helped. An ampersand gives the people who came to both, and a minus gives who missed both.

00:30 Python thanks Ben Carter and Gus Tanaka, and reminds Fatima Sheikh.

00:37 Port it line by line. Each set display becomes a new Set() around an array. JavaScript has no set comprehension, so a for...of loop over the plots adds each holder.

00:51 The three operator lines carry over as they are, with const in front. Before you port the report lines, log what the three lines give: helped and both first, then missed and how many people helped.

01:06 In Python, helped holds eight names, and both holds two. What does JavaScript log for helped and both? Your turn: pause the video and predict.

01:23 Run it, and JavaScript logs 0 and 0. Missed is NaN, and the size of helped is undefined. There’s no error and no warning.

01:35 Every line ran, and none of them made a Set.

01:40 Why numbers? In both languages, the bar and the ampersand between two whole numbers are bitwise operators. Python’s set type gives them a second meaning. JavaScript has no way for a type to do that, so they only work on numbers.

01:58 Each Set turns into text, "[object Set]", which isn’t a number, so it’s NaN. Bitwise operators count NaN as zero, and zero or zero is zero.

02:10 Minus subtracts, and a Set minus zero is NaN. Helped is the number zero, and a number has no size, so it’s undefined.

02:22 JavaScript names each set operation as a method instead. For the bar, call union() on spring, and pass autumn. For the ampersand, call intersection().

02:35 Run the check again. Helped is a new Set of all eight names, spring’s first, then autumn’s two new ones. Both holds Ben Carter and Gus Tanaka. Each method leaves spring and autumn alone, as Python’s operators do.

02:52 These methods are recent additions, and Node 26 has them all.

02:58 The second check still logs NaN for the minus. For the minus, call difference() on holders, and pass helped. It keeps the holders who aren’t in helped.

03:09 Run it again, and that’s one name, Fatima Sheikh. Watch out for lists. Python’s union() method takes a list too, but these methods want a Set.

03:22 Given an array, they stop with a TypeError about its size, so wrap the array in a new Set() first.

03:31 Now the report lines. A JavaScript Set keeps insertion order, which is why helped listed spring’s names first. Python’s sets promise no order. Printed as it is, both can come out either way from one run to the next, so Python’s script sorts before it joins.

03:51 The port sorts too: insertion order follows the lists, not the alphabet. A Set has no toSorted(), so spread it into an array, sort the copy, and join it with commas.

04:03 Run it, and JavaScript prints Python’s two lines.

04:08 The rule to keep: JavaScript has no set operators. Call union(), intersection() and difference(), and each returns a new Set.

04:19 And sort a Set’s members before you print them.

04:23 Up next is the text lesson Comparing Sets With isSubsetOf(), where the committee asks its own questions about the work days.

Become a Member to join the conversation.