Bada's Coding

Bada's Coding

Parameter Destructuring with Default Parameter

Bada's photo
·Oct 30, 2022·

1 min read


Please check the codes below.

function fn({ k1 = 1 }) {
  return k1;

console.log(fn('Put whatever you want here.'));

The conclusion of the codes above is this.

An error occurs if these things are the parameters.

nothing, undefined, null

These parameters will return '1'.

any strings (even an empty string), any numbers (even 0 and NaN), any booleans (even false), any objects (even an empty object)

Preventing the Error

In the above, errors were made if the parameters were nothing, undefined and null.

It can be solved by giving a default parameter to the wrapping object parameter.

function fn({ k1 = 1 } = {}) {
  return k1;

console.log(fn()); // 1
Share this