Member-only story
Stop Writing Unsafe JavaScript: Optional Chaining and Nullish Coalescing Explained
Webstack
6 min read10 hours ago
Press enter or click to view image in full size
Every developer who works with JavaScript has seen this error in production: Cannot read properties of undefined (reading 'name') .
It usually happens when you expect an object or property to exist, but the server returns null or undefined . A simple API change or missing field can break your UI and leave users looking at a blank page.
For years, developers wrote long chains of if statements or used logical && operators to prevent these crashes. Modern JavaScript gives us better tools: Optional Chaining ( ?. ) and Nullish Coalescing ( ?? ) .
Used together, these operators let you handle missing data safely without cluttering your codebase.…