JS Coding Questions Logo
JS Coding Questions
#309💼 Interview💻 Code

What are default parameters

Advertisement

728x90

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
1//ES5
2
3var calculateArea = function (height, width) {
4  height = height || 50;
5  width = width || 60;
6
7  return width * height;
8};
9
10console.log(calculateArea()); //300

The default parameters makes the initialization more simpler,

javascript
1//ES6
2
3var calculateArea = function (height = 50, width = 60) {
4  return width * height;
5};
6
7console.log(calculateArea()); //300

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 53

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
309of476