Operator Assignment Shorthand (operator-assignment)

JavaScript provides shorthand operators that combine variable assignment and some simple mathematical operations. For example, x = x + 4 can be shortened to x += 4. The supported shorthand forms are as follows:

 Shorthand | Separate
-----------|------------
 x += y    | x = x + y
 x -= y    | x = x - y
 x *= y    | x = x * y
 x /= y    | x = x / y
 x %= y    | x = x % y
 x <<= y   | x = x << y
 x >>= y   | x = x >> y
 x >>>= y  | x = x >>> y
 x &= y    | x = x & y
 x ^= y    | x = x ^ y
 x |= y    | x = x | y

Rule Details

This rule enforces use of the shorthand assignment operators by requiring them where possible or prohibiting them entirely.

Options

This rule has two options: always and never. The default is always.

“always”

"operator-assignment": ["error", "always"]

This mode enforces use of operator assignment shorthand where possible.

The following are examples of valid patterns:

/*eslint operator-assignment: ["error", "always"]*/

x = y;
x += y;
x = y * z;
x = (x * y) * z;
x[0] /= y;
x[foo()] = x[foo()] % 2;
x = y + x; // `+` is not always commutative (e.g. x = "abc")

The following patterns are considered problems and should be replaced by their shorthand equivalents:

/*eslint operator-assignment: ["error", "always"]*/

x = x + y;
x = y * x;
x[0] = x[0] / y;
x.y = x.y << z;

“never”

"operator-assignment": ["error", "never"]

This mode warns on any use of operator assignment shorthand.

The following are examples of valid patterns:

/*eslint operator-assignment: ["error", "never"]*/

x = x + y;
x.y = x.y / a.b;

The following patterns are considered problems and should be written out fully without the shorthand assignments:

/*eslint operator-assignment: ["error", "never"]*/

x *= y;
x ^= (y + z) / foo();

When Not To Use It

Use of operator assignment shorthand is a stylistic choice. Leaving this rule turned off would allow developers to choose which style is more readable on a case-by-case basis.

Version

This rule was introduced in ESLint 0.10.0.

Resources