Skip to content

Commit 1cc4727

Browse files
Completed ES6 ✨
1 parent 188db0b commit 1cc4727

File tree

9 files changed

+766
-0
lines changed

9 files changed

+766
-0
lines changed

Advanced-JavaScript/14. ES6/10.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,76 @@ Unlike Maps, WeakMaps are not iterabl
8282
*/
8383

8484

85+
86+
/*
87+
JavaScript WeakMap
88+
The WeakMap is similar to a Map. However, WeakMap can only contain objects as keys. For example,
89+
90+
const weakMap = new WeakMap();
91+
console.log(weakMap); // WeakMap {}
92+
93+
let obj = {};
94+
95+
// adding object (element) to WeakMap
96+
weakMap.set(obj, 'hello');
97+
98+
console.log(weakMap); // WeakMap {{} => "hello"}
99+
Run Code
100+
When you try to add other data types besides objects, WeakMap throws an error. For example,
101+
102+
const weakMap = new WeakMap();
103+
104+
// adding string as a key to WeakMap
105+
weakMap.set('obj', 'hello');
106+
Run Code
107+
// throws error
108+
// TypeError: Attempted to set a non-object key in a WeakMap
109+
WeakMap Methods
110+
WeakMaps have methods get(), set(), delete(), and has(). For example,
111+
112+
const weakMap = new WeakMap();
113+
console.log(weakMap); // WeakMap {}
114+
115+
let obj = {};
116+
117+
// adding object (element) to WeakMap
118+
weakMap.set(obj, 'hello');
119+
120+
console.log(weakMap); // WeakMap {{} => "hello"}
121+
122+
// get the element of a WeakMap
123+
console.log(weakMap.get(obj)); // hello
124+
125+
// check if an element is present in WeakMap
126+
console.log(weakMap.has(obj)); // true
127+
128+
// delete the element of WeakMap
129+
console.log(weakMap.delete(obj)); // true
130+
131+
console.log(weakMap); // WeakMap {}
132+
Run Code
133+
WeakMaps Are Not iterable
134+
Unlike Maps, WeakMaps are not iterable. For example,
135+
136+
const weakMap = new WeakMap();
137+
console.log(weakMap); // WeakMap {}
138+
139+
let obj = {};
140+
141+
// adding object (element) to WeakMap
142+
weakMap.set(obj, 'hello');
143+
144+
145+
// looping through WeakMap
146+
for (let i of weakMap) {
147+
148+
console.log(i); // TypeError
149+
}
150+
151+
152+
153+
154+
155+
156+
157+
*/

Advanced-JavaScript/14. ES6/11.js

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
/*
2+
avaScript Set and WeakSet
3+
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,
99+
100+
const weakSet = new WeakSet();
101+
console.log(weakSet); // WeakSet {}
102+
103+
let obj = {
104+
message: 'Hi',
105+
sendMessage: true
106+
}
107+
108+
// adding object (element) to WeakSet
109+
weakSet.add(obj);
110+
111+
console.log(weakSet); // WeakSet {{message: "Hi", sendMessage: true}}
112+
Run Code
113+
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']);
217+
218+
let result = difference(setA, setB);
219+
220+
console.log(result);
221+
Run Code
222+
Output
223+
224+
Set {"mango", "orange"}
225+
Example: Set Subset Operation
226+
// perform subset operation
227+
// true if all elements of set b is in set a
228+
function subset(setA, setB) {
229+
for (let i of setB) {
230+
if (!setA.has(i)) {
231+
return false
232+
}
233+
}
234+
return true
235+
}
236+
237+
// two sets of fruits
238+
let setA = new Set(['apple', 'mango', 'orange']);
239+
let setB = new Set(['apple', 'orange']);
240+
241+
let result = subset(setA, setB);
242+
243+
console.log(result);
244+
Run Code
245+
Output
246+
247+
true
248+
249+
250+
251+
252+
253+
254+
*/

0 commit comments

Comments
 (0)