How it works...

In the preceding recipe, we rely on the has method on the Set class that implements the difference operation. An element of a difference must appear in the first set but not the second. When looping over the first set, we know that the element is part of the first. Next, we simply have to check whether it is in the second using the has method. If this method returns false, then we know that the element is in the difference.

An important distinction between difference and the other two operations we've seen (union and intersection) is that it is not commutative, that is, the order of arguments matters. You can see in the preceding recipe that the result of the difference function changes depending on which set is being differentiated ...

Get ECMAScript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.