Skip to main content
2 of 4
added 638 characters in body
Ori Drori
  • 195.8k
  • 32
  • 244
  • 235

Set a default empty object, and add a default value for each field:

const { outerObj:{ field1 = 22, field2 = 33} = {}} = objToDestructure;

Example:

const { outerObj: { field1 = 22, field2 = 33} = {}} = {}

console.log(field1, field2)

And you can also set a default for the outerObj (as you tried before):

<!-- begin snippet: js hide: false console: true babel: false -->

Note: the difference between 1 and 2 is that if outerObj exists, the default in 2 won't be used. The defaults in 1 would be used as long as the individual properties don't exist.

Ori Drori
  • 195.8k
  • 32
  • 244
  • 235