Q16: How do Optional Chaining (`?.`) and Nullish Coalescing (`??`) work?

Difficulty:EasyTopic:Operators
  • Optional Chaining (?.): Safely accesses nested object properties or methods. If the left-hand operand is null or undefined, evaluation stops and returns undefined without throwing a TypeError.
  • Nullish Coalescing (??): A logical operator that returns its right-hand operand only when the left-hand operand evaluates to null or undefined (unlike ||, which triggers on all falsy values like 0 or "").
Key Takeaways
  • ?. prevents nested property access runtime errors.

  • ?? checks explicitly for null or undefined, ignoring falsy values like 0 or false.