Assignment operators shorthand

JavaScriptreadability

JavaScript has many different assignment operators that are shorthand for longer versions. += is one of the more common ones. Most other calculations also have an assignment operator.

// regular assignment assigns the right value to the variable on the left
a = b

// we can shorten the code if the variable is itself part of the calculation
a = a + 5    // is the same as:
a += 5

// these are some of the available shorthand assignment operators
a += b    // a = a + b       addition assignment
a -= b    // a = a - b       subtraction assignment
a *= b    // a = a * b       multiplication assignment
a /= b    // a = a / b       division assignment
a %= b    // a = a % b       remainder assignment

a &&= b   // a = a && b      logical AND assignment
a ||= b   // a = a || b      logical OR assignment