babel / proposals

✍️ Tracking the status of Babel's implementation of TC39 proposals (may be out of date)
https://github.com/tc39/proposals
432 stars 39 forks source link

Nullish Coalescing (Stage 3) #14

Closed hzoo closed 4 years ago

hzoo commented 7 years ago

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

https://twitter.com/littledan/status/908032146484469761 https://github.com/babel/babylon/pull/742/files#r142008197

jridgewell commented 7 years ago

logicalCOALESCE? Too much of a mouthful? logicalQQ?

gisenberg commented 7 years ago

I started referring to this as the existential operator in the slides.

hzoo commented 7 years ago

@gisenberg feel free to edit the title/etc (if your in the tc39 team it gives you write access to this repo)

ljharb commented 7 years ago

(see https://github.com/gisenberg/proposal-nullary-coalescing/issues/3)

azz commented 6 years ago

Parser PR: babel/babylon#761, follow-up: babel/babylon#762 Transform PR: babel/babel#6483

vjpr commented 6 years ago

What is the status of this? Is it ready to use?

azz commented 6 years ago

Yes, just enable the @babel/plugin-proposal-nullish-coalescing-operator plugin.

https://www.npmjs.com/package/@babel/plugin-proposal-nullish-coalescing-operator

hzoo commented 4 years ago

Yep https://github.com/babel/babel/tree/master/packages/babel-plugin-proposal-nullish-coalescing-operator