I'm looking for a way to do undefined coalescing in javascript with booleans. I'm used to doing the following, for, say, positive integers:
var x = i1 || i2 || i3 || i4;
This is a 'slick' way, but is not what I'm after. I'm looking for the equivalent of ??
in C#.
var b1 = undefined;
var b2 = null;
var b3 = false;
var b4 = true;
var x = b1 || b2 || b3 || b4; //x == true (b4)
However, I want the above to 'stop' on false (coalesce on undefined or null, but NOT false). The application I'm doing this for is similar to the following:
var threshold = argv[2] ?? process.env.threshold ?? config.threshold ?? 0.3;
var verbose = isTrue(argv[3] ?? process.env.verbose ?? config.verbose ?? false);
I'm looking for a slick way do to this, similar to the ||
operator, not:
var verbose = false;
if (config.verbose !== undefined && config.verbose !== null) verbose = isTrue(config.verbose);
if (process.env.verbose !== undefined && process.env.verbose !== null) verbose = isTrue(process.env.verbose);
if (argv[3] !== undefined && argv[3] !== null) verbose = isTrue(argv[3]);
Similarly, I'd want a '0' threshold to be accepted for the threshold, and any undefined or null values skipped.