logo

Optional Chaining

Explain `?.` operator for safe property access and how it reduces guard code.

Short explanation

Optional chaining (?.) allows safe navigation through nested objects without verbose checks for null or undefined.

Syntax example

const street = user?.address?.street;

How JS handles it internally

The operator short-circuits when encountering null or undefined, preventing further property access and avoiding exceptions.

FAQ

Q: Is optional chaining a replacement for validation?

A: It reduces boilerplate for property access but validation may still be needed for business logic.