assignmentOperatorShorthands
Prefer logical assignment operator shorthand expressions.
✅ This rule is included in the ts stylistic preset.
ES2021 introduced logical assignment operators (||=, &&=, ??=) that combine logical operations with assignment.
These shorthand operators make code more concise and express intent more clearly.
For example, a = a || b can be written as a ||= b.
This pattern is common for providing default values or conditionally updating variables.
Examples
Section titled “Examples”let value = 0;value = value || 1;let config: Config | undefined;config = config ?? getDefaultConfig();let enabled = false;enabled = enabled && isFeatureAvailable();let value = 0;value ||= 1;let config: Config | undefined;config ??= getDefaultConfig();let enabled = false;enabled &&= isFeatureAvailable();Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If you need to support environments that do not have ES2021 support, you may need to avoid using logical assignment operators. You may also prefer the more explicit expanded form if you find it more readable.
Further Reading
Section titled “Further Reading”- MDN: Logical OR assignment (||=)
- MDN: Logical AND assignment (&&=)
- MDN: Nullish coalescing assignment (??=)
Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- ESLint:
logical-assignment-operators
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.