ES6 Default Parameters in nested objects – JavaScript



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
Updated on: 2020-11-09T09:06:58+05:30

572 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements