DEV Community

Toolloom
Toolloom

Posted on • Originally published at toolloom.com on

Stop Fearing Null Pointers with Optional Chaining

If you're tired of writing long logical chains just to check if a nested property exists, optional chaining (?.) is your best friend. It short-circuits the evaluation, returning undefined instead of throwing an error if a reference is null or undefined.

Combine it with the nullish coalescing operator (??) to provide a sensible default value. This pattern makes your code significantly more readable and much less brittle when you're dealing with unpredictable API responses or complex state objects.

Top comments (0)