Skip to content

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 |=.

let value = 0;
value = value + 1;
let count = 5;
count = count * 2;
const object = { property: 0 };
object.property = object.property - 1;

This rule is not configurable.

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.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.