0 votes
in JavaScript by
What are default parameters in Javascript?

1 Answer

0 votes
by

In ES5, we need to depend on logical OR operators to handle default values of function parameters. Whereas in ES6, Default function parameters feature allows parameters to be initialized with default values if no value or undefined is passed. Let's compare the behavior with an examples,

//ES5
var calculateArea = function (height, width) {
  height = height || 50;
  width = width || 60;

  return width * height;
};
console.log(calculateArea()); //300

The default parameters makes the initialization more simpler,

//ES6
var calculateArea = function (height = 50, width = 60) {
  return width * height;
};

console.log(calculateArea()); //300

Related questions

0 votes
asked Mar 9 in JavaScript by DavidAnderson
0 votes
asked Oct 24, 2023 in JavaScript by DavidAnderson
...