Optional Chaining & Safe Access
Optional chaining (?.) prevents errors when accessing properties that might not exist. It's essential for handling incomplete or variable data structures.
The Problem
Basic Optional Chaining
Optional Chaining with Arrays
Optional Method Calls
Nullish Coalescing (??)
Combine ?. with ?? for default values:
Exercise: Safe Navigation
Exercise: Array Safety
Key Points
?.stops and returnsundefinedif value is null/undefined- Works with properties:
obj?.prop - Works with arrays:
arr?.[0] - Works with methods:
fn?.() - Combine with
??for default values ??only replaces null/undefined, not falsy values

