InterviewHubPRO
Section Overview
Q313

Question 313 of 521

Part 5: ES6+ Modern Syntax, Classes, Modules & Destructuring (254 - 328)

60%
JavaScript Internals (521 Qs)•Part 5: ES6+ Modern Syntax, Classes, Modules & Destructuring (254 - 328)

What are default parameters

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,

javascript
  //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,

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

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