DEV Community

Toolloom
Toolloom

Posted on • Originally published at toolloom.com on

Clean Up Your Code with Optional Chaining

If you find yourself writing lengthy defensive checks like if (data && data.user && data.user.profile) just to safely access a deeply nested property, stop! Modern JavaScript introduced Optional Chaining (?.) specifically for this scenario. Now you can write data?.user?.profile?.name. If any part of that chain is null or undefined, the expression simply evaluates to undefined instead of throwing a massive runtime error. It’s cleaner, safer, and makes your data access logic much more readable immediately.

Top comments (0)