ES6 Default Parameters in nested objects – JavaScript

JavascriptWeb DevelopmentFront End ScriptsObject Oriented Programming

Yes, you can pass default parameters in nested objects.

Following is the code −

Example

Following is the code −

function callBackFunctionDemo({ cl: { callFunctionName = "callBackFunction", values = 100 } = {} } = {}) {
   console.log(callFunctionName);
   console.log(values);
}
//This will print the default value. // 100
callBackFunctionDemo();
//This will print the given value. //500
callBackFunctionDemo({ cl: { values: 500 } });

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo296.js.

Output

This will produce the following output on console −

PS C:\Users\Amit\javascript-code> node demo296.js
callBackFunction
100
callBackFunction
500
raja
Updated on 09-Nov-2020 09:06:58

Advertisements