operatorAssignmentShorthand
Prefer assignment operator shorthand where possible.
✅ This rule is included in the ts stylistic presets.
JavaScript provides shorthand operators that combine variable assignment and mathematical operations. These operators make code more concise and express intent more clearly.
For example, x = x + y can be written as x += y.
This rule applies to the operators +=, -=, *=, /=, %=, **=, <<=, >>=, >>>=, &=, ^=, and |=.
Examples
Section titled “Examples”let value = 0;value = value + 1;let count = 5;count = count * 2;const object = { property: 0 };object.property = object.property - 1;let value = 0;value += 1;let count = 5;count *= 2;const object = { property: 0 };object.property -= 1;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 all assignment operators, you may need to avoid using some shorthand operators. You may also prefer the more explicit expanded form if you find it more readable in specific cases.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- Biome:
useShorthandAssign - ESLint:
operator-assignment - Oxlint:
eslint/operator-assignment
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.