Using destructuring assignment instead of ternary operator for variable assignment

Posted on November 28, 2021

I used to write something like this when I wanted to assign a variable from a property object. It's a common pattern that uses the Ternary Operator of js:

const path = current ? current.path : null

That's because in case of current undefined, the runtime error is handled.

The same assignment can be done using the Destructive Assignment, and it's nicer:

const { path } = current || {}

Of course, it works if the object current contains the property path.

If you want to use a different variable name, you must rely to the first example.