💻 Tech
One of the gotchas in JS is getting the default value if variable is an empty string. Two of ways to get a default value is the nullish coalescing operator ??
and the logical OR operator ||
. The former only works if the value is undefined
or null
, while the latter if the value is “falsy” (i.e., false
, empty string, undefined
, and null
).
let x = '';
console.log(x ?? 'default1');
console.log(x || 'default2');