Demo 1
Create new Javascript file named demo1.js. This file contains code demo as below:
let names = new Set();
names.add('Name 1');
names.add('Name 2');
names.add('Name 2');
names.add('Name 3');
names.add('Name 3');
console.log('Size: ' + names.size);
console.log('Check Name 3 Already: ' + names.has('Name 3'));
console.log('Name List');
for (let name of names) {
console.log(name);
console.log('----------');
}
console.log('Name List');
names.forEach(name => {
console.log(name);
console.log('----------');
});
names.delete('Name 3');
console.log('Size: ' + names.size);
names.clear();
console.log('Size: ' + names.size);
Run Demo 1
Use node demo1.js statement run code demo
Size: 3
Check Name 3 Already: true
Name List
Name 1
----------
Name 2
----------
Name 3
----------
Name List
Name 1
----------
Name 2
----------
Name 3
----------
Size: 2
Size: 0
Demo 2
Create new Javascript file named demo2.js. This file contains code demo as below:
class Product {
constructor(id, name, price, quantity) {
this.id = id;
this.name = name;
this.price = price;
this.quantity = quantity;
}
toString() {
return 'Id: ' + this.id + '\nName: ' + this.name + '\nPrice: ' + this.price + '\nQuantity: ' + this.quantity;
}
}
let product1 = new Product("p01", "name 1", 4, 2);
let product2 = new Product("p02", "name 2", 11, 3);
let product3 = new Product("p03", "name 3", 7, 8);
let product4 = new Product("p04", "name 4", 15, 3);
let products = new Set();
products.add(product1);
products.add(product2);
products.add(product3);
products.add(product4);
console.log('Product List');
products.forEach(p => {
console.log(p.toString());
console.log('--------------------------');
})
console.log('Product List');
for (let product of products) {
console.log(product.toString());
console.log('--------------------------');
}
Run Demo 2
Use node demo2.js statement run code demo
Product List
Id: p01
Name: name 1
Price: 4
Quantity: 2
--------------------------
Id: p02
Name: name 2
Price: 11
Quantity: 3
--------------------------
Id: p03
Name: name 3
Price: 7
Quantity: 8
--------------------------
Id: p04
Name: name 4
Price: 15
Quantity: 3
--------------------------
Product List
Id: p01
Name: name 1
Price: 4
Quantity: 2
--------------------------
Id: p02
Name: name 2
Price: 11
Quantity: 3
--------------------------
Id: p03
Name: name 3
Price: 7
Quantity: 8
--------------------------
Id: p04
Name: name 4
Price: 15
Quantity: 3
--------------------------
Demo 3
Create new Javascript file named demo3.js. This file contains code demo as below:
class Product {
constructor(id, name, price, quantity) {
this.id = id;
this.name = name;
this.price = price;
this.quantity = quantity;
}
toString() {
return 'Id: ' + this.id + '\nName: ' + this.name + '\nPrice: ' + this.price + '\nQuantity: ' + this.quantity;
}
}
let product1 = new Product("p01", "name 1", 4, 2);
let product2 = new Product("p02", "name 2", 11, 3);
let product3 = new Product("p03", "name 3", 7, 8);
let product4 = new Product("p04", "name 4", 15, 3);
let products = new Set([product1, product2, product3, product4]);
console.log('Product List');
products.forEach(p => {
console.log(p.toString());
console.log('--------------------------');
})
console.log('Product List');
for (let product of products) {
console.log(product.toString());
console.log('--------------------------');
}
Run Demo 3
Use node demo3.js statement run code demo
Product List
Id: p01
Name: name 1
Price: 4
Quantity: 2
--------------------------
Id: p02
Name: name 2
Price: 11
Quantity: 3
--------------------------
Id: p03
Name: name 3
Price: 7
Quantity: 8
--------------------------
Id: p04
Name: name 4
Price: 15
Quantity: 3
--------------------------
Product List
Id: p01
Name: name 1
Price: 4
Quantity: 2
--------------------------
Id: p02
Name: name 2
Price: 11
Quantity: 3
--------------------------
Id: p03
Name: name 3
Price: 7
Quantity: 8
--------------------------
Id: p04
Name: name 4
Price: 15
Quantity: 3
--------------------------