DEV Community

Cover image for A beginner friendly guide to optional chaining in Javascript
Dev
Dev

Posted on

A beginner friendly guide to optional chaining in Javascript

Optional Chaining has become one of my favorite ES2020 features.

Optional chaining allows you to safely access the nested object properties without the hassle to check validity of each one of them.
Instead of using && to check for null or undefined, directly use ?.
When you want to use an object's property you must check if it exists otherwise you may get a type error.
Thus in ES2020 we have ?. operator which does the job for you.

Alt Text

So when are you going to use optional chaining next?

Top comments (2)

Collapse
 
whitewcallum580 profile image
Callum White

I didn't think I needed this till I recently discovered it. I now use it all the time.

Collapse
 
softik profile image
Vyacheslav

pretty useful thing