DEV Community

Toolloom
Toolloom

Posted on • Originally published at toolloom.com on

Say Goodbye to 'Undefined' Errors with Optional Chaining

We've all fought the "Cannot read property 'x' of undefined" battle. Instead of writing long chains of if (user && user.profile && user.profile.name), use the optional chaining operator (?.). Writing user?.profile?.name will safely return undefined if any part of the chain is missing, preventing your app from crashing. It makes your code much cleaner and more resilient.

Top comments (0)