JS TutorialLesson #76

JavaScript Set Logic

[JavaScript 2025](/js/js2025.asp) is fully supported in all modern browsers since May 2025:

Browser Support

Set Logic is an ECMAScript 2025 feature.

JavaScript 2025 is fully supported in all modern browsers since May 2025:

Chrome 136Edge 136Firefox 129Safari 18.2Opera 120
Apr 2025Apr 2025Aug 2024Des 2024May 2025

The union() Method

The union() method returns the union of two sets.

The union() method returns a new set containing the elements which are in this set, or in the argument set, or in both:

Example

Example Code

javascript
Try it Yourself
const A = new Set(['a','b','c']);

const B = new Set(['b','c','d']);

const C = A.union(B);

The intersection() Method

The intersection() method returns the intersection of two sets.

The intersection() method returns a new set containing the elements which are in this set and in the argument set:

Example

Example Code

javascript
Try it Yourself
const A = new Set(['a','b','c']);

const B = new Set(['b','c','d']);

const C = A.intersection(B);

The difference() Method

The difference() method returns the difference between two sets.

The difference() method returns a new set containing elements which are in this set but not in the argument set:

Example

Example Code

javascript
Try it Yourself
const A = new Set(['a','b','c']);

const B = new Set(['b','c','d']);

const C = A.difference(B);

The symmetricDifference() Method

The symmetricDifference() method returns the symmetric difference between to sets.

The symmetricDifference() method returns a new set containing elements which are in this set or in the argument set, but not in both:

Example

Example Code

javascript
Try it Yourself
const A = new Set(['a','b','c']);

const B = new Set(['b','c','d']);

const C = A.symmetricDifference(B);

The isSubsetOf() Method

The isSubsetOf() method returns true if all elements in this set is also elements in the argument set:

Example

Example Code

javascript
Try it Yourself
const A = new Set(['a','b','c']);

const B = new Set(['b','c','d']);

let answer = A.isSubsetOf(B);

The isSupersetOf() Method

The isSupersetOf() method returns true if all elements in the argument set are also in this set:

Example

Example Code

javascript
Try it Yourself
const A = new Set(['a','b','c']);

const B = new Set(['b','c','d']);

let answer = A.isSupersetOf(B);

The isDisjointFrom() Method

The isDisjointFrom() method returns true if this set has no elements in common with the argument set:

Example

Example Code

javascript
Try it Yourself
const A = new Set(['a','b','c']);

const B = new Set(['b','c','d']);

let answer = A.isDisjointFrom(B);

Learn More: > JavaScript Sets > JavaScript Set Methods > JavaScript Weak Sets > JavaScript Set Reference > JavaScript Maps

Interactive Code Examples

JS Example 1

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
const A = new Set(['a','b','c']);

const B = new Set(['b','c','d']);



const C = A.union(B);

JS Example 2

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
const A = new Set(['a','b','c']);

const B = new Set(['b','c','d']);



const C = A.intersection(B);

JS Example 3

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
const A = new Set(['a','b','c']);

const B = new Set(['b','c','d']);



const C = A.difference(B);

JS Example 4

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
const A = new Set(['a','b','c']);

const B = new Set(['b','c','d']);



const C = A.symmetricDifference(B);

JS Example 5

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
const A = new Set(['a','b','c']);

const B = new Set(['b','c','d']);



let answer = A.isSubsetOf(B);

JS Example 6

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
const A = new Set(['a','b','c']);

const B = new Set(['b','c','d']);



let answer = A.isSupersetOf(B);