Day 3: Optional Chaining & Nullish Coalescing
Daily Objective
Section titled “Daily Objective”Write resilient, error-free code by safely accessing deep properties with ?. and setting fallbacks with ??.
1. Syntax Examples
Section titled “1. Syntax Examples”const user = { profile: { address: null } };
// Optional Chaining (?.) prevents Cannot read properties of undefinedconst street = user?.profile?.address?.street ?? 'No address provided';
// Nullish Coalescing (??) only triggers for null or undefined (unlike || which triggers for 0 and "")const count = 0;const displayCount = count ?? 10; // 0const fallbackCount = count || 10; // 10 (undesired fallback for 0!)Daily Hands-on Practice (70%)
Section titled “Daily Hands-on Practice (70%)”- Compare
||vs??on empty strings""and number0. - Inspect deep APIs with missing keys using
?..