@babel/plugin-proposal-logical-assignment-operators

NOTE: This plugin is included in @babel/preset-env, in ES2021

Example

In

  1. a ||= b;
  2. obj.a.b ||= c;
  3. a &&= b;
  4. obj.a.b &&= c;

Out

  1. var _obj$a, _obj$a2;
  2. a || (a = b);
  3. (_obj$a = obj.a).b || (_obj$a.b = c);
  4. a && (a = b);
  5. (_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)

  1. a ??= b;
  2. obj.a.b ??= c;
  1. var _a, _obj$a, _obj$a$b;
  2. (_a = a) !== null && _a !== void 0 ? _a : (a = b);
  3. (_obj$a$b = (_obj$a = obj.a).b) !== null && _obj$a$b !== void 0
  4. ? _obj$a$b
  5. : (_obj$a.b = c);

Installation

  1. npm install --save-dev @babel/plugin-proposal-logical-assignment-operators

Usage

  1. {
  2. "plugins": ["@babel/plugin-proposal-logical-assignment-operators"]
  3. }

Via CLI

  1. babel --plugins @babel/plugin-proposal-logical-assignment-operators script.js

Via Node API

  1. require("@babel/core").transformSync("code", {
  2. plugins: ["@babel/plugin-proposal-logical-assignment-operators"],
  3. });

References