TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
^= (bitwise XOR assignment) operator performs a bitwise exclusive OR operation on the binary representations of its left and right operands, assigning the computed result back to the left operand.
leftOperand = leftOperand ^ rightOperand), the ^= operator evaluates the leftOperand exactly once. This is a critical distinction when the left operand contains expressions with side effects. For example, arr[i++] ^= 2 increments i only once, whereas arr[i++] = arr[i++] ^ 2 would evaluate the index and increment i twice.
Execution Mechanics
When the^= operator is invoked, the TypeScript runtime (via JavaScript) executes the following sequence based on the operand types:
- Numeric Conversion & Truncation:
- For
numberoperands: Both operands are evaluated and implicitly converted to 32-bit signed integers using two’s complement representation. Any fractional components are truncated. - For
bigintoperands: Both operands are evaluated at arbitrary precision without being truncated to 32 bits.
- For
- Bitwise Comparison: The operator compares the operands bit by bit. It yields a
1in a given bit position if the corresponding bits of the operands differ, and a0if they are identical.0 ^ 0 = 00 ^ 1 = 11 ^ 0 = 11 ^ 1 = 0
- Assignment: The newly constructed numeric value (either a 32-bit integer or a
bigint) is assigned to the memory location of theleftOperand.
Step-by-Step Evaluation
a(5) is represented as...0000 0101b(3) is represented as...0000 0011
TypeScript Compiler Rules
TypeScript enforces strict type checking on bitwise assignment operators to prevent unintended runtime coercions.- Valid Assignment Targets: The left operand must be a valid assignment target (l-value). This includes standalone variables, object properties, and array elements.
- Left Operand Type Constraints: The left operand must resolve to type
number,bigint, orany. In modern TypeScript (5.0+), anenumvariable is not a valid left operand for^=. While the bitwise XOR operation itself accepts enums, it evaluates to anumber. This resultingnumbercannot be implicitly assigned back to anenumtype. - Operand Mixing Rules:
numberandbiginttypes are strictly incompatible and cannot be mixed in the same operation.enummembers are treated as numeric values and can be used as the right operand when the left operand is anumber.anybypasses all strict type matching constraints, allowing operations with any right operand (though standard JavaScript runtime coercion rules will apply).
Master TypeScript with Deep Grasping Methodology!Learn More





