DEV Community

Toolloom
Toolloom

Posted on • Originally published at toolloom.com on

Cleaner Logic with Nullish Coalescing

Stop writing deeply nested if statements or long && chains just to check if a property exists. Use optional chaining (?.) to safely access nested properties without throwing an error if a parent is null. Pair this with the nullish coalescing operator (??) to provide a fallback value only when the result is null or undefined. Unlike the logical OR (||), it won't trigger on "falsy" values like 0 or an empty string, which prevents a lot of common UI bugs.

Top comments (0)