The addition assignment (Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
+=) operator evaluates the right operand, adds it to the value of the left operand, and assigns the resulting value back to the left operand. The specific operation performed—numeric addition or string concatenation—is dictated by the data types of the operands.
LeftOperand = LeftOperand + RightOperand, with the strict architectural distinction that the LeftOperand reference is evaluated only once.
Evaluation Mechanics and Type Coercion
Because JavaScript is dynamically typed, the+= operator relies on the ECMAScript internal ToPrimitive algorithm and standard addition semantics to resolve the operation.
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. Mixing a Number operand with a BigInt operand is not permitted and will throw a TypeError.
String (after any necessary primitive coercion), the operator forces string concatenation. The non-string operand is implicitly coerced into a string.
Object (including Array and Date), JavaScript attempts to convert it to a primitive value before applying the += operator. It does this by passing a "default" hint to the internal ToPrimitive algorithm. The resolution follows a specific hierarchy:
- If the object implements a
[Symbol.toPrimitive]()method, that method is invoked first. - If no such method exists, standard objects prioritize the
valueOf()method. IfvalueOf()does not return a primitive, it falls back to thetoString()method. Dateobjects exhibit the exact opposite behavior: they prioritizetoString()and fall back tovalueOf().
true to 1, false to 0, and null to 0. undefined coerces to NaN (Not-a-Number).
Master JavaScript with Deep Grasping Methodology!Learn More





