Logical Assignment Operators S4
- Stage: Stage 4
- Status: Finished
- ECMAScript edition: ES2021
- Synchronized: Aug 28, 2026
- 中文译文 · Source repository
This proposal introduces logical assignment operators (||=, &&=, ?=) that combine logical operators with assignment expressions, allowing for concise defaults and updates while leveraging short-circuit semantics to avoid unnecessary setter calls.
The README below comes from the upstream repository and may contain outdated stage or status metadata. Use the proposal details above as the current source of truth.
proposal-logical-assignment
A proposal to combine Logical Operators and Assignment Expressions:
Status
Current Stage: 4
Champions
- Justin Ridgewell (@jridgewell)
- Hemanth HM (@hemanth)
Motivation
Convenience operators, inspired by Ruby's. We already have a dozen mathematical assignment operators, but we don't have ones for the often used logical operators.
If statements work, but terseness would be nice. Especially when dealing with deep property assignment:
With this proposal, we get terseness and we don't have to suffer from setter calls:
Semantics
The logical assignment operators function a bit differently than their mathematical assignment friends. While math assignment operators always trigger a set operation, logical assignment embraces their short-circuiting semantics to avoid it when possible.
In most cases, the fact that the set operation is short-circuited has no
observable impact beyond performance. But when it has side effects, it
is often desirable to avoid it when appropriate. In the following
example, if the .innerHTML setter was triggered uselessly, it could
result in the loss of state (such as focus) that is not serialized in
HTML:
See discussion of short-circuit semantics in #3. It also highlights differences already present in mathematical assignment operators in code like obj.deep[key++] += 1 vs obj.deep[key] = obj.deep[key++] + 1.
Related
- Ruby's logical operators
- CoffeeScript
- C#'s null coalescing assignment
- My very first Babel PR (back when it was still 6to5). 😄