DEV Community

Discussion on: 5 Uses for the Spread Operator

Collapse
 
genocideaon profile image
Pranat

What is the way you always use to prevent error when data is null or undefined?
For my case, I always face this when 'initVar' is null or undefined. Or at lease 'aaa' is null or undefined.

const {aaa: {bbb, ccc}} = initVar

Collapse
 
laurieontech profile image
Laurie

So that example is desrructhring assignment. I look at that in a different post which is linked at the bottom of this one.

Collapse
 
docx profile image
Lukáš Doležal • Edited

Maybe you could actually use the spread to start with default values and override with initVar:

const { aaa: { bbb, ccc } } = {
  aaa: { 
    bbb: "default bbb", 
    ccc: "default ccc", 
    ...(initVar && initVar.aaa) 
  }
}

But not sure if that is really useful in maybe more complex deconstructions? :D