JavaScript Set symmetricDifference()
Syntax & Examples
Set.symmetricDifference() method
The symmetricDifference() method of the Set object in JavaScript takes another set as an argument and returns a new set containing elements which are in either the original set or the given set, but not in both.
Syntax of Set.symmetricDifference()
The syntax of Set.symmetricDifference() method is:
symmetricDifference(other)This symmetricDifference() method of Set takes a set and returns a new set containing elements which are in either this set or the given set, but not in both.
Parameters
| Parameter | Optional/Required | Description |
|---|---|---|
other | required | The set to compare with the original set. |
Return Type
Set.symmetricDifference() returns value of type Set.
✐ Examples
1 Using symmetricDifference() to find unique elements between two sets of numbers
In JavaScript, we can use the symmetricDifference() method to find unique elements between two sets of numbers.
For example,
- Create a new Set object
setAwith initial values 1, 2, and 3. - Create another Set object
setBwith initial values 3, 4, and 5. - Use the
symmetricDifference()method to find the unique elements betweensetAandsetB, storing the result insymDiffSet. - Log the
symDiffSetto the console usingconsole.log().
JavaScript Program
const setA = new Set([1, 2, 3]);
const setB = new Set([3, 4, 5]);
const symDiffSet = setA.symmetricDifference(setB);
console.log(symDiffSet);Output
Set { 1, 2, 4, 5 }2 Using symmetricDifference() with sets of strings
In JavaScript, we can use the symmetricDifference() method to find unique elements between two sets of strings.
For example,
- Create a new Set object
setAwith initial values 'apple', 'banana', and 'cherry'. - Create another Set object
setBwith initial values 'banana', 'cherry', and 'date'. - Use the
symmetricDifference()method to find the unique elements betweensetAandsetB, storing the result insymDiffSet. - Log the
symDiffSetto the console usingconsole.log().
JavaScript Program
const setA = new Set(['apple', 'banana', 'cherry']);
const setB = new Set(['banana', 'cherry', 'date']);
const symDiffSet = setA.symmetricDifference(setB);
console.log(symDiffSet);Output
Set { 'apple', 'date' }3 Using symmetricDifference() to find unique elements between sets of objects
In JavaScript, we can use the symmetricDifference() method to find unique elements between two sets of objects.
For example,
- Create a new Set object
setAwith initial objects representing different people. - Create another Set object
setBwith some overlapping objects. - Use the
symmetricDifference()method to find the unique elements betweensetAandsetB, storing the result insymDiffSet. - Log the
symDiffSetto the console usingconsole.log().
JavaScript Program
const person1 = { name: 'John' };
const person2 = { name: 'Jane' };
const person3 = { name: 'Doe' };
const setA = new Set([person1, person2]);
const setB = new Set([person2, person3]);
const symDiffSet = setA.symmetricDifference(setB);
console.log(symDiffSet);Output
Set { { name: 'John' }, { name: 'Doe' } }Summary
In this JavaScript tutorial, we learned about symmetricDifference() method of Set: the syntax and few working examples with output and detailed explanation for each example.