JavaScript Set difference()
Syntax & Examples
Set.difference() method
The difference() method of the Set object in JavaScript takes another set as an argument and returns a new set containing elements that are present in the original set but not in the given set.
Syntax of Set.difference()
The syntax of Set.difference() method is:
difference(other)
This difference() method of Set takes a set and returns a new set containing elements in this set but not in the given set.
Parameters
Parameter | Optional/Required | Description |
---|---|---|
other | required | The set whose elements will be excluded from the original set. |
Return Type
Set.difference() returns value of type Set
.
✐ Examples
1 Using difference() to find the difference between two sets of numbers
In JavaScript, we can use the difference()
method to find the difference between two sets of numbers.
For example,
- Create a new Set object
setA
with initial values 1, 2, and 3. - Create another Set object
setB
with initial values 2 and 3. - Use the
difference()
method to find the difference betweensetA
andsetB
, storing the result indifferenceSet
. - Log the
differenceSet
to the console usingconsole.log()
.
JavaScript Program
const setA = new Set([1, 2, 3]);
const setB = new Set([2, 3]);
const differenceSet = setA.difference(setB);
console.log(differenceSet);
Output
Set { 1 }
2 Using difference() with sets of strings
In JavaScript, we can use the difference()
method to find the difference between two sets of strings.
For example,
- Create a new Set object
setA
with initial values 'apple', 'banana', and 'cherry'. - Create another Set object
setB
with initial values 'banana' and 'cherry'. - Use the
difference()
method to find the difference betweensetA
andsetB
, storing the result indifferenceSet
. - Log the
differenceSet
to the console usingconsole.log()
.
JavaScript Program
const setA = new Set(['apple', 'banana', 'cherry']);
const setB = new Set(['banana', 'cherry']);
const differenceSet = setA.difference(setB);
console.log(differenceSet);
Output
Set { 'apple' }
3 Using difference() to find the difference between sets of objects
In JavaScript, we can use the difference()
method to find the difference between two sets of objects.
For example,
- Create a new Set object
setA
with initial objects representing different people. - Create another Set object
setB
with some overlapping objects. - Use the
difference()
method to find the difference betweensetA
andsetB
, storing the result indifferenceSet
. - Log the
differenceSet
to 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 differenceSet = setA.difference(setB);
console.log(differenceSet);
Output
Set { { name: 'John' } }
Summary
In this JavaScript tutorial, we learned about difference() method of Set: the syntax and few working examples with output and detailed explanation for each example.