Default Parameters in ECMAScript

Create new Javascript file named demo1.js. This file contains code demo as below:

function default1(x = 1, y = 2, z = 3) {
  console.log(x, y, z);
}

default1(5);
default1(5, 6);

Use node demo1.js statement run code demo

5 2 3
5 6 3




Create new Javascript file named demo2.js. This file contains code demo as below:

function default2 (x = 1, y = 2, z = 3) {
  console.log(x, y, z);
}
default2(undefined, 6, 7);

Use node demo2.js statement run code demo

1 6 7

Create new Javascript file named demo3.js. This file contains code demo as below:

function getFullPrice(price) {
  return price * 1.13;
}

function getValue(price, pricePlusTax = getFullPrice(price)) {
  console.log('Price: ' + price.toFixed(2));
  console.log('Price Plus Tax: ' + pricePlusTax.toFixed(2));
}

getValue(25);
console.log('---------------------');
getValue(25, 30);




Use node demo3.js statement run code demo

Price: 25.00
Price Plus Tax: 28.25
---------------------
Price: 25.00
Price Plus Tax: 30.00