kenpeter
kenpeter

Reputation: 8284

How to set default values when destructing an object?

In javascript, to destruct multiple props, how do I set prop1, prop2, prop3 with default value individually?

const {
    prop1,
    prop2,
    prop3
  } = obj.props;

Upvotes: 1

Views: 447

Answers (1)

dork
dork

Reputation: 4578

The same way you would set default values in function params, with an = sign.

const {
  prop1 = 'default value',
  prop2,
  prop3,
} = obj.props;

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Default_values

Upvotes: 2

Related Questions