Demo 1
Create new Javascript file named demo1.js. This file contains code demo as below:
function myFunc(a, b, ...args) {
console.log('Length: ' + args.length);
console.log(args);
}
myFunc(1, 2);
myFunc(1, 2, 3, 4);
Run Demo 1
Use node demo1.js statement run code demo
Length: 0
[]
Length: 2
[ 3, 4 ]
Demo 2
Create new Javascript file named demo2.js. This file contains code demo as below:
function sum(...numbers) {
let s = 0;
for (let i = 0; i < numbers.length; i++) {
s += numbers[i];
}
return s;
}
console.log('Sum 1: ' + sum(3, 2));
console.log('Sum 2: ' + sum(3, 2, 6));
console.log('Sum 3: ' + sum(3, 2, 9, 10));
Run Demo 2
Use node demo2.js statement run code demo
Sum 1: 5
Sum 2: 11
Sum 3: 24
Demo 3
Create new Javascript file named demo3.js. This file contains code demo as below:
function sum(...numbers) {
let s = 0;
for (let number of numbers) {
s += number;
}
return s;
}
console.log('Sum 1: ' + sum(3, 2));
console.log('Sum 2: ' + sum(3, 2, 6));
console.log('Sum 3: ' + sum(3, 2, 9, 10));
Run Demo 3
Use node demo3.js statement run code demo
Sum 1: 5
Sum 2: 11
Sum 3: 24
Demo 4
Create new Javascript file named demo4.js. This file contains code demo as below:
class King {
constructor(name, country) {
this.name = name;
this.country = country;
}
getDescription() {
return `${this.name} leads ${this.country}`;
}
}
var details = ['Name 1', 'Country 1'];
var king = new King(...details);
console.log(king.getDescription());
Run Demo 4
Use node demo4.js statement run code demo
Name 1 leads Country 1