The Nullish Coalescing Operator (??) and the Logical OR Operator (||) are used to provide default values when a variable is null, undefined, or falsy.
??)Returns the right-hand value if the left-hand value is null or undefined.
let user;
let name = user ?? 'Guest';
console.log(name); // 'Guest'
||)Returns the right-hand value if the left-hand value is falsy (0, '', null, undefined, false, NaN).
let user = '';
let name = user || 'Guest';
console.log(name); // 'Guest'
0, false, and '' as falsy, while Nullish Coalescing only treats null and undefined as nullish.
Click the button to see Nullish Coalescing and Logical OR in action: