recently reached to a release candidate,
One of the new additions are logical assignment operators (
which combine the logical operations(&&, || or ??) with assignment(=).
Let’s take a look at some examples of these and their use cases.
Logical nullish assignment (??=)
Logical nullish assignment operator assigns a value to a
variable if it is currently nullish (
This can come in handy in cases where
we’d want to fill out missing properties or options.
Logical AND assignment (&&=)
Logical AND assignment operator assigns
a value to a variable if it is currently
This helps us simplify some of the checks for
presence/truthiness before assigning values.
Logical OR assignment (||=)
Logical OR assignment operation assigns a value to a variable if it is currently
Same as logical AND,
this helps us simplify some of the checks for presence/truthiness before assigning values.
Using ES12/ES2021 features in codebase
The new ES2021 features are
by recent versions of major browsers.
To enable these features in old browsers, we need to do the below setup.
Install Babel packages:
babel.config.json file at the root of the project.
.browserlistrc file at the root of the project to
specify the target browsers for Babel to transform code in -
Run the below command.
It will transform the files in the
src folder into a compatible version for old browsers
and output in the
The newly added logical assignment operators,
help us simplify conditional assignments and evaluations,
in a better way.