DEV Community

Cover image for 7 New JavaScript Set Methods
Zachary Lee
Zachary Lee

Posted on • Originally published at webdeveloper.beehiiv.com on

4

7 New JavaScript Set Methods

TL;DR: JavaScript’s Set data structure has added 7 new APIs that are very useful and can reduce the need for libraries like lodash. They are:

  • intersection() returns a new set with elements in both this set and the given set.

  • union() returns a new set with all elements in this set and the given set.

  • difference() returns a new set with elements in this set but not in the given set.

  • symmetricDifference() returns a new set with elements in either set, but not in both.

  • isSubsetOf() returns a boolean indicating if all elements of a set are in a specific set.

  • isSupersetOf() returns a boolean indicating if all elements of a set are in a specific set.

  • isDisjointFrom() returns a boolean indicating if this set has no elements in common with a specific set.


1. Set.prototype.intersection()

The intersection() method of Set instances takes a set and returns a new set containing elements in both this set and the given set.

Venn diagram:

Example:

2. Set.prototype.union()

The union() method of Set instances takes a set and returns a new set containing elements which are in either or both of this set and the given set.

Venn diagram:

Example:

3. Set.prototype.difference()

The difference() method of Set instances takes a set and returns a new set containing elements in this set but not in the given set.

Venn diagram:

Example:

4. Set.prototype.symmetricDifference()

The symmetricDifference() method of Set instances takes a set and returns a new set containing elements which are in either this set or the given set, but not in both.

Venn diagram:

Example:

5. Set.prototype.isSubsetOf()

The isSubsetOf() method of Set instances takes a set and returns a boolean indicating if all elements of this set are in the given set.

Venn diagram:

Example:

6. Set.prototype.isSupersetOf()

The isSupersetOf() method of Set instances takes a set and returns a boolean indicating if all elements of the given set are in this set.

Venn diagram:

Example:

7. Set.prototype.isDisjointFrom()

The isDisjointFrom() method of Set instances takes a set and returns a boolean indicating if this set has no elements in common with the given set.

Venn diagram:

Example:


If you find my content helpful, please consider subscribing. I send a weekly newsletter every Sunday with the latest web development updates. Thanks for your support!

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post

Top comments (0)

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Best practices for optimal infrastructure performance with Magento

Running a Magento store? Struggling with performance bottlenecks? Join us and get actionable insights and real-world strategies to keep your store fast and reliable.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️