Loading topic
union, intersection, and difference to Set. They build on a few rules that Set has followed since ES2015: how it decides two values are the same, how it orders its elements, and how it behaves when you iterate over it. If you can predict those rules6 min readunion, intersection, difference, and symmetricDifference each build a brand-new Set and return it. They never change the set you call them on, and they never change the set you pass in. The later chapters on each method build o6 min readSet.prototype.union takes two sets and returns a new Set holding every element found in either one. Before ES2025 you wrote this yourself with spread syntax. Now it is a single method call with defined ordering and defined error behavior.5 min readSet.prototype.intersection answers one question: which elements are in both sets? It returns a new Set that holds only those shared elements. This chapter covers how the method works, the order its results come in, how it compares tricky values, and the mistakes developers make most often.5 min readSet, use spread syntax and Array.prototype.filter, and know that set methods return a new Set instead of changing the original. You should also have worked through union and intersection, because difference is easiest to understand next t5 min readunion, intersection, and difference work from the earlier chapters. You should also know that Set methods return a new Set and leave their inputs alone, and that elements are compared with SameValueZero.6 min readunion, intersection, difference, and symmetricDifference. Often you don't need a new collection. You need a yes-or-no answer. Does this user have every permission the page requires? Do two tag lists share anything? Is one list of IDs5 min readunion, intersection, and difference in earlier chapters, and that you understand the basic difference between primitive values and object references. Here the focus is on one question: when do two elements count as "the same6 min readfor...of or spread always follows that order. When union, intersection, difference, or symmetricDifference builds a new Set, the new Set has an order too. Most of the time that order is easy to predict. One method 6 min readSet as the argument to union, intersection, and the other methods. That is the common case, but the methods do not require it. The argument only has to be set-like. This chapter explains what counts as set-like, how the methods use the argument interna5 min readsize, has, keys), and how spread and Array.prototype.filter work. Here the question is practical: where can you call union and its siblings safely, how can your code tell whether6 min readunion, intersection, difference, symmetricDifference, and the three predicates behave, and that you are comfortable with Array.prototype.filter, map, and reduce. Ea6 min read