Daily Software Tips & Tricks
Bite-sized knowledge to improve your coding skills daily.
Stop Fearing the "Undefined" Error
March 27, 2026
We've all seen the dreaded "Cannot read property of undefined" error. Instead of nesting five `if` statements or using complex logical AND operators to check if a deeply nested object exists, use optional chaining (`?.`). It tells JavaScript to stop looking and return `undefined` immediately if it hits a null or undefined value in the chain.
You can even use it for function calls, like `data.onSave?.()`. It makes your code significantly cleaner and more readable. Combine it with the nullish coalescing operator (`??`) to provide a default value, and you’ve got a robust way to handle data that might not always be there.