Optional chaining and ??
?. stops at null or undefined instead of throwing. ?? gives a default only for null or undefined.
JavaScript · Optional chaining and ??Beginner
const user = { profile: { address: null }, count: 0 };
const city = user.profile?.address?.city; // undefined, no error
const phone = user.contact?.phone ?? 'not given'; // 'not given'
const n = user.count ?? 10; // 0 (not 10: 0 is a real value)
const bad = user.count || 10; // 10 (|| treats 0 as missing)
const fn = user.save?.(); // call only if it existsoptional chainingnullishsafe access