Skip to main content
InterviewHubPRO
Section Overview
Q313

Question 313 of 536

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

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

What are default parameters

junior LevelCode ExamplejavascriptES6+ Modern Syntax

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