DEV Community

Toolloom
Toolloom

Posted on • Originally published at toolloom.com on

Stop the "Undefined" Crashes with Optional Chaining

We've all been there: checking if (user && user.profile && user.profile.name) to avoid a crash. Modern JavaScript and TypeScript give us the ?. operator, which makes this much cleaner. Using user?.profile?.name will simply return undefined if any part of the chain is nullish, keeping your code readable and your console free of those pesky "cannot read property of undefined" errors.

Top comments (0)