You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In this tutorial, you will learn about JavaScript Sets and WeakSets with the help of examples.
4
+
5
+
The JavaScript ES6 has introduced two new data structures, i.e Set and WeakSet.
6
+
7
+
Set is similar to an array that allows us to store multiple items like numbers, strings, objects, etc. However, unlike an array, a set cannot contain duplicate values.
8
+
9
+
Create JavaScript Set
10
+
To create a Set, you need to use the new Set() constructor. For example,
11
+
12
+
// create Set
13
+
const set1 = new Set(); // an empty set
14
+
console.log(set1); // Set {}
15
+
16
+
// Set with multiple types of value
17
+
const set2 = new Set([1, 'hello', {count : true}]);
18
+
console.log(set2); // Set {1, "hello", {count: true}}
19
+
Run Code
20
+
When duplicate values are passed to a Set object, the duplicate values are excluded.
21
+
22
+
// Set with duplicate values
23
+
const set3 = new Set([1, 1, 2, 2]);
24
+
console.log(set3); // Set {1, 2}
25
+
Run Code
26
+
Access Set Elements
27
+
You can access Set elements using the values() method and check if there is an element inside Set using has() method. For example,
28
+
29
+
const set1 = new Set([1, 2, 3]);
30
+
31
+
// access the elements of a Set
32
+
console.log(set1.values()); // Set Iterator [1, 2, 3]
33
+
Run Code
34
+
You can use the has() method to check if the element is in a Set. For example,
35
+
36
+
const set1 = new Set([1, 2, 3]);
37
+
38
+
// check if an element is in Set
39
+
console.log(set1.has(1));
40
+
Run Code
41
+
Adding New Elements
42
+
You can add elements to a Set using the add() method. For example,
43
+
44
+
const set = new Set([1, 2]);
45
+
console.log(set.values());
46
+
47
+
// adding new elements
48
+
set.add(3);
49
+
console.log(set.values());
50
+
51
+
// adding duplicate elements
52
+
// does not add to Set
53
+
set.add(1);
54
+
console.log(set.values());
55
+
Run Code
56
+
Output
57
+
58
+
Set Iterator [1, 2]
59
+
Set Iterator [1, 2, 3]
60
+
Set Iterator [1, 2, 3]
61
+
Removing Elements
62
+
You can use the clear() and the delete() method to remove elements from a Set.
63
+
64
+
The delete() method removes a specific element from a Set. For example,
65
+
66
+
const set = new Set([1, 2, 3]);
67
+
console.log(set.values()); // Set Iterator [1, 2, 3]
68
+
69
+
// removing a particular element
70
+
set.delete(2);
71
+
console.log(set.values()); // Set Iterator [1, 3]
72
+
Run Code
73
+
The clear() method removes all elements from a Set. For example,
74
+
75
+
const set = new Set([1, 2, 3]);
76
+
console.log(set.values()); // Set Iterator [1, 2, 3]
77
+
78
+
// remove all elements of Set
79
+
set.clear();
80
+
console.log(set.values()); // Set Iterator []
81
+
Run Code
82
+
Iterate Sets
83
+
You can iterate through the Set elements using the for...of loop or forEach() method. The elements are accessed in the insertion order. For example,
84
+
85
+
const set = new Set([1, 2, 3]);
86
+
87
+
// looping through Set
88
+
for (let i of set) {
89
+
console.log(i);
90
+
}
91
+
Run Code
92
+
Output
93
+
94
+
1
95
+
2
96
+
3
97
+
JavaScript WeakSet
98
+
The WeakSet is similar to a Set. However, WeakSet can only contain objects whereas a Set can contain any data types such as strings, numbers, objects, etc. For example,
When you try to add other data types besides objects, WeakSet throws an error. For example,
114
+
115
+
// trying to add string to WeakSet
116
+
weakSet.add('hello');
117
+
118
+
// throws error
119
+
// TypeError: Attempted to add a non-object key to a WeakSet
120
+
console.log(weakSet);
121
+
Run Code
122
+
WeakSet Methods
123
+
WeakSets have methods add(), delete(), and has(). For example,
124
+
125
+
const weakSet = new WeakSet();
126
+
console.log(weakSet); // WeakSet {}
127
+
128
+
const obj = {a:1};
129
+
130
+
// add to a weakSet
131
+
weakSet.add(obj);
132
+
console.log(weakSet); // WeakSet {{a: 1}}
133
+
134
+
// check if an element is in Set
135
+
console.log(weakSet.has(obj)); // true
136
+
137
+
// delete elements
138
+
weakSet.delete(obj);
139
+
console.log(weakSet); // WeakSet {}
140
+
Run Code
141
+
WeakSets Are Not iterable
142
+
Unlike Sets, WeakSets are not iterable. For example,
143
+
144
+
const weakSet = new WeakSet({a:1});
145
+
146
+
// looping through WeakSet
147
+
for (let i of weakSet) {
148
+
149
+
// TypeError
150
+
console.log(i);
151
+
}
152
+
Run Code
153
+
Mathematical Set Operations
154
+
In JavaScript, Set does not provide built-in methods for performing mathematical operations such as union, intersection, difference, etc. However, we can create programs to perform those operations.
155
+
156
+
Example: Set Union Operation
157
+
// perform union operation
158
+
// contain elements of both sets
159
+
function union(a, b) {
160
+
let unionSet = new Set(a);
161
+
for (let i of b) {
162
+
unionSet.add(i);
163
+
}
164
+
return unionSet
165
+
}
166
+
167
+
// two sets of fruits
168
+
let setA = new Set(['apple', 'mango', 'orange']);
169
+
let setB = new Set(['grapes', 'apple', 'banana']);
170
+
171
+
let result = union(setA, setB);
172
+
173
+
console.log(result);
174
+
Run Code
175
+
Output
176
+
177
+
Set {"apple", "mango", "orange", "grapes", "banana"}
178
+
Example: Set Intersection Operation
179
+
// perform intersection operation
180
+
// elements of set a that are also in set b
181
+
function intersection(setA, setB) {
182
+
let intersectionSet = new Set();
183
+
184
+
for (let i of setB) {
185
+
if (setA.has(i)) {
186
+
intersectionSet.add(i);
187
+
}
188
+
}
189
+
return intersectionSet;
190
+
}
191
+
192
+
// two sets of fruits
193
+
let setA = new Set(['apple', 'mango', 'orange']);
194
+
let setB = new Set(['grapes', 'apple', 'banana']);
195
+
196
+
let result = intersection(setA, setB);
197
+
198
+
console.log(result);
199
+
Run Code
200
+
Output
201
+
202
+
Set {"apple"}
203
+
Example: Set Difference Operation
204
+
// perform difference operation
205
+
// elements of set a that are not in set b
206
+
function difference(setA, setB) {
207
+
let differenceSet = new Set(setA)
208
+
for (let i of setB) {
209
+
differenceSet.delete(i)
210
+
}
211
+
return differenceSet
212
+
}
213
+
214
+
// two sets of fruits
215
+
let setA = new Set(['apple', 'mango', 'orange']);
216
+
let setB = new Set(['grapes', 'apple', 'banana']);
0 commit comments