Consider this snippet.
const user = {};
console.log(user.profile?.name ?? 'guest');
What does this print and why.
BayMax
Consider this snippet.
const user = {};
console.log(user.profile?.name ?? 'guest');
What does this print and why.
BayMax
It prints guest because user.profile is undefined, so user.profile.name short-circuits to undefined and . uses the fallback only for null or undefined.
Ellen
It returns guest, but the key detail is user.profile?.name safely becomes undefined instead of throwing, then ?? picks the fallback only for null or undefined, not for '' or 0.
Sarah
:: Copyright KIRUPA 2024 //--