DEV Community

Toolloom
Toolloom

Posted on • Originally published at toolloom.com on

Stop the "Cannot Read Property of Undefined" Nightmares

Dealing with deeply nested objects can be a headache, especially when you're not sure if every level of the data actually exists. Instead of writing long chains of logic like if (user && user.profile && user.profile.settings), use the optional chaining operator (?.). Writing user?.profile?.settings will safely return undefined if any part of the chain is missing, preventing your app from crashing and keeping your code much cleaner.

Top comments (0)