Today I Learned

Optional chaining

I used to use lodash to access chained properties, to prevent app from crashing when one of the properties in the chain was undefined. Bartek Boruta has shown me a nice trick, which I known from other languages, but I was not sure if it exists in JS.

Instead of:

const city = get(user, '')

you can use

const city = user?.address?.city,

which will give the same result - city’s name or an undefined property.