Title: Is there a slick way to do undefined coalescence in JavaScript?
Tags: javascript;null;undefined;null-coalescing-operator
Question: 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 &amp;&amp; config.verbose !== null) verbose = isTrue(config.verbose);
if (process.env.verbose !== undefined &amp;&amp; process.env.verbose !== null) verbose = isTrue(process.env.verbose);
if (argv[3] !== undefined &amp;&amp; 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.


Here is another answer: The slickest way (without adding extra functions) that I can think of is:
var x = [b1, b2, b3, b4].find(x =&gt; x !== null &amp;&amp; x !== undefined);
If this is to be used over and over, define a function that does all the work for you:
function coalesce() {
return [].find.call(arguments, x =&gt; x !== null &amp;&amp; x !== undefined);
var x = coalesce(b1, b2, b3, b4);
NB: ES2015 syntax used and functionality used above!  It'd be trivial to rewrite in "old" syntax and shim the Array.prototype.any function for older browsers.
Also note that without native support for a coalescing operator that all of the parameters above will be evaluated - you lose the advantage of the short circuiting of || or ?? wherein the right hand expression isn't evaluated if the left hand expression is returned.
Comment for this answer: Hmm, if adding functions is the only way to go, can I define my own null coalescing operator to `??`? What are my choices of symbols if not?
Comment for this answer: http://stackoverflow.com/questions/476436/is-there-a-null-coalescing-operator-in-javascript
Comment for this answer: @Ehryk you can't define your own operators in JS.  See answer update though for an improved function.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:11/25/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<javascript><null><undefined><null-coalescing-operator>', 'Title': 'Is there a slick way to do undefined coalescence in JavaScript?', 'ViewCount': 1906, 'id': 33926965, 'user_AboutMe': '<p>I\'m a Computer (Web) Programmer/Analyst based in Anchorage, AK and Minneapolis, MN.  I use (among other things) ASP.NET, C# and SQL Server.</p>\n\n<p>I build things. Bicycles, computers, websites, guitars, cars, motorcycles, sound sytems... lots of things.</p>\n\n<ul>\n<li>Resume: <a href="http://ericmenze.com" rel="nofollow">http://ericmenze.com</a></li>\n<li>Personal Website: <a href="http://ehryk.com" rel="nofollow">http://ehryk.com</a></li>\n<li>Pause your videos at specific locations: <a href="http://pauseforlater.com" rel="nofollow">http://pauseforlater.com</a></li>\n<li>Calculate and build spoked bicycle wheels: <a href="http://wheelspoking.com" rel="nofollow">http://wheelspoking.com</a></li>\n<li>See activity specific analysis of your GPX Files: <a href="http://gpxdataanalyzer.com" rel="nofollow">http://gpxdataanalyzer.com</a></li>\n<li>Tool to open command/powershell prompts from any location (Windows): <a href="https://github.com/Ehryk/ContextMenuTools" rel="nofollow">https://github.com/Ehryk/ContextMenuTools</a></li>\n</ul>\n', 'user_AccountId': 948111.0, 'user_DisplayName': 'Ehryk', 'user_Reputation': 1891.0, 'user_Views': 241.0, 'user_id': 974907.0}}