Skip to main content
The += (addition assignment) operator evaluates the addition or string concatenation of the right operand and the current value of the left operand, subsequently assigning the evaluated result back to the left operand.
While conceptually similar to the expanded assignment expression leftOperand = leftOperand + rightOperand, the += operator evaluates the leftOperand exactly once. This distinction is critical when the left operand contains side effects (such as an incrementing index or a getter method). In an expression like arr[i++] += 5, the index i is evaluated and incremented only once, whereas the expanded form arr[i++] = arr[i++] + 5 would evaluate the increment operation twice, yielding a completely different result.

Type Resolution and Behavior

In TypeScript, the behavior of the += operator is dictated by the types of the operands. The TypeScript compiler (tsc) enforces strict type checking during this operation to prevent implicit type coercion anomalies common in standard JavaScript.
  1. Numeric Addition: If both operands are strictly of type number, or if both operands are strictly of type bigint, the operator performs standard mathematical addition. TypeScript strictly prohibits mixing number and bigint in the same operation.
  2. String Concatenation: If either the left or right operand is of type string, the operator performs string concatenation.
  3. Type Assignability: TypeScript verifies that the return type of the evaluated expression is assignable to the declared type of the leftOperand. If it is not, the compiler emits a type error.

Syntax Visualization

Evaluation Semantics:
Valid Numeric Assignment:
Valid String Concatenation:
Compiler Error (Mixing BigInt and Number):
Compiler Error (Type Assignability Mismatch):
Tired of Poor TypeScript Skills? Fix That With Deep Grasping!Learn More