EVERY THING HERE IS ABOUT PROGRAMMING.

Ahmad Karimzade

Sep 13 5 min

Optional Chaining In JS Will Save Your Nerves

Most of you had much problem before with objects validation like this:

if (obj.avatar && obj.avatar.small && obj.avatar.small.url) {}

in old way you should write so dirty and long conditions to validate object.

Question is how we can solve it?

Answer is Optional Chaining. Optional Chaining is one of the best Javascript features released in ES2020. mission of this features is solve long and dirty object conditions.

lets take a look how it works: Without Optional Chaining:

if (obj.avatar && obj.avatar.small && obj.avatar.small.url) {}

With Optional Chaining:

if (obj?.avatar?.small?.url) {}

with Optional Chaining we only wrote one condition instead of three diffrent conditions. when you put ? sign after property name and before . actually you say if the proptery is doesn't exist break the condition by throwing undefined.

Look at this example:

let obj = { avatar: null }

console.log(obj.avatar.small.url) // TypeError: Cannot read properties of undefined

console.log(obj.avatar?.small?.url) // undefined

in summary Optional Chaining checks every property and if that is null or undefined will break and throws undefined.

RECENT POSTSSee All
Optional Chaining In JS Will Save Your Nerves
Sep 14 5 min
Optional Chaining In JS Will Save Your Nerves

What Is Optional Chaining And How Its Would Help You To Write Better Code

How To Publish Package On NPM
Oct 18 15 min
How To Publish Package On NPM

How To Publish Your Utils On NPM