So the double pipe ||
is the logical OR operator.
I want to create a function where it can be either foo
, or when thats flase (or falsy?) then it should use a default fallback. In this case 'bar'.
const foo = 0;
const bar = 'somethingElse';
const qux = foo || bar;
console.log(qux); // returns 'somethingElse'
Now the above example will work unless foo is 0. Since JS will interpret that als falsy.
Now the following seems to be working:
// ...
const qux = typeof foo !== 'undefined' ? foo : bar;
But I'm wondering if there is a better solution. What would be the best way of handling such a scenario?