Member-only story
I Made My Code 10x Cleaner With Optional Chaining
The one operator that ended my “Cannot read property of undefined” error.
Have you ever tried to add a conditional check while accessing a nested value in JavaScript? Checking the properties in JavaScript is necessary, as one missing value can give you an error “Cannot read properties of undefined,” and your application can crash.
Read it for free here.
Here is the code you have used so far.
if(employee&&employee.profile&&employee.profile.image&&employee.profile.image.url){
console.log("employee.profile.url");
}While using the && operator, a single undefined error can crash your application.
Its working is also the same as drilling a hole inside the earth and letting the water come out. And if there’s a barrier ahead, it’s all over.
Problem With The Above Code
You can clearly see that the above code is
- very repetitive
- Hard to read and maintain
- Easy to introduce bugs
The most important thing is that, in large API-driven projects, it is how this code will look.








