Q16: How do Optional Chaining (`?.`) and Nullish Coalescing (`??`) work?
- Optional Chaining (
?.): Safely accesses nested object properties or methods. If the left-hand operand isnullorundefined, evaluation stops and returnsundefinedwithout throwing a TypeError. - Nullish Coalescing (
??): A logical operator that returns its right-hand operand only when the left-hand operand evaluates tonullorundefined(unlike||, which triggers on all falsy values like0or"").
Key Takeaways
?.prevents nested property access runtime errors.??checks explicitly fornullorundefined, ignoring falsy values like0orfalse.