If you are still writing deeply nested if-statements or long chains of logical ANDs to check if a property exists, it is time to embrace the optional chaining operator (?.). It allows you to read the value of a property located deep within a chain of connected objects without having to manually validate each reference.
Combine this with the nullish coalescing operator (??) to provide a fallback value, and your code becomes significantly more readable. It turns five lines of defensive checks into a single, elegant expression that is much easier for the next developer to maintain.
Top comments (0)