Pedro Henrique Alves Cruz’s Post

One JavaScript feature that quietly makes your code cleaner and safer: 👉 Optional Chaining (?.) Before: const city = user && user.address && user.address.city; Or worse… nested if checks everywhere. Now: const city = user?.address?.city; What’s actually happening? 👉 If anything in the chain is null or undefined, JavaScript just stops—and returns undefined instead of crashing. No more: “Cannot read property 'x' of undefined” It gets even better: user?.getProfile?.(); Calls the function only if it exists No more “is not a function” errors But here’s the part many people miss: 👉 Optional chaining hides errors if you overuse it If something should exist and doesn’t, ?. will quietly return undefined… and your bug becomes harder to notice. Rule of thumb: • Use ?. for optional data (API responses, external input) • Avoid it for required logic (core business rules) JavaScript gives you tools to write safer code. 👉 The real skill is knowing when not to use them. Do you use optional chaining everywhere—or selectively? #softwareengineering #javascript #codequality #webdevelopment

To view or add a comment, sign in

Explore content categories