Description
Champions: @gisenberg
Spec Repo: https://github.com/gisenberg/proposal-nullary-coalescing
First presented at the July 2017 meeting: slides
Moved to Stage 1 at the Sept 2017 meeting: slides
Syntax
a ?? b
When performing optional property access in a nested structure in conjunction with the optional chaining operator, it is often desired to provide a default value if the result of that property access is null or undefined. At present, a typical way to express this intent in JavaScript is by using the || operator.
so most use const variable = a || b
but a falsy value like 0 or '' or false
will unintentionally return b
as the value.
Notes:
While this proposal specifically calls out null and undefined values, the intent is to provide a complementary operator to the optional chaining operator. This proposal will update to match the semantics of that operator.
Parsing
- a new node type name (operator)?
- pluginName?
https://twitter.com/littledan/status/908032146484469761
https://github.com/babel/babylon/pull/742/files#r142008197