The bitwise XOR 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 performs a bitwise exclusive OR operation between its left and right operands and assigns the resulting value back to the left operand.
Syntax
x = x ^ y, the compound assignment operator (^=) evaluates the left-hand reference exactly once. This distinction is critical when the left operand contains side effects. For example, arr[i++] ^= 3 evaluates the index i only once, whereas arr[i++] = arr[i++] ^ 3 evaluates i twice.
Execution Mechanics
When the^= operator is evaluated, the JavaScript engine performs the following sequence of operations:
- Evaluation: The left-hand reference is evaluated once, followed by the right-hand expression.
- Type Coercion (ToNumeric): Both operands undergo the
ToNumericabstract operation. This resolves the operands to eitherNumberorBigInttypes. (Note: MixingNumberandBigIntoperands throws aTypeError). - Bitwise Preparation:
- For Numbers: Both operands are implicitly coerced from IEEE 754 double-precision floats into 32-bit signed integers (
ToInt32) in two’s complement format. Fractional components are truncated. - For BigInts: The engine skips the 32-bit coercion entirely.
BigIntvalues operate with arbitrary precision and are not truncated to 32 bits.
- For Numbers: Both operands are implicitly coerced from IEEE 754 double-precision floats into 32-bit signed integers (
- XOR Logic: The engine compares the operands bit by bit. For each bit position, the resulting bit is set to
1if the corresponding bits of the operands are different. If the bits are identical (both0or both1), the resulting bit is set to0. - Assignment: The newly calculated value is assigned to the reference on the left side of the operator.
Truth Table
| Left Bit | Right Bit | Resulting Bit |
|---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
Step-by-Step Evaluation
Number Operands
5is converted to a 32-bit integer:...0000 01013is converted to a 32-bit integer:...0000 0011- The XOR operation is applied vertically:
- The variable
ais reassigned the value6.
BigInt Operands
BigInt values, the XOR operation is applied across their arbitrary-precision binary representations without any 32-bit boundary truncation.
Non-Numeric Operands
If the operands are not numbers orBigInts, the ToNumeric abstract operation coerces them. For standard values, this falls back to Number() coercion rules before applying the ToInt32 conversion. Values that coerce to NaN (like undefined or non-numeric strings) are converted to 0 during the 32-bit integer conversion.
Master JavaScript with Deep Grasping Methodology!Learn More





