@babel/plugin-proposal-logical-assignment-operators
NOTE: This plugin is included in
@babel/preset-env
, in ES2021
Example
In
a ||= b;
obj.a.b ||= c;
a &&= b;
obj.a.b &&= c;
Out
var _obj$a, _obj$a2;
a || (a = b);
(_obj$a = obj.a).b || (_obj$a.b = c);
a && (a = b);
(_obj$a2 = obj.a).b && (_obj$a2.b = c);
With Nullish Coalescing
While using the
@babel/plugin-proposal-nullish-coalescing-operator
plugin (included in@babel/preset-env
)
a ??= b;
obj.a.b ??= c;
var _a, _obj$a, _obj$a$b;
(_a = a) !== null && _a !== void 0 ? _a : (a = b);
(_obj$a$b = (_obj$a = obj.a).b) !== null && _obj$a$b !== void 0
? _obj$a$b
: (_obj$a.b = c);
Installation
npm install --save-dev @babel/plugin-proposal-logical-assignment-operators
Usage
With a configuration file (Recommended)
{
"plugins": ["@babel/plugin-proposal-logical-assignment-operators"]
}
Via CLI
babel --plugins @babel/plugin-proposal-logical-assignment-operators script.js
Via Node API
require("@babel/core").transformSync("code", {
plugins: ["@babel/plugin-proposal-logical-assignment-operators"],
});