Skip to main content

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.

The bitwise XOR assignment (^=) 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 ^= y
While logically similar to 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:
  1. Evaluation: The left-hand reference is evaluated once, followed by the right-hand expression.
  2. Type Coercion (ToNumeric): Both operands undergo the ToNumeric abstract operation. This resolves the operands to either Number or BigInt types. (Note: Mixing Number and BigInt operands throws a TypeError).
  3. 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. BigInt values operate with arbitrary precision and are not truncated to 32 bits.
  4. XOR Logic: The engine compares the operands bit by bit. For each bit position, the resulting bit is set to 1 if the corresponding bits of the operands are different. If the bits are identical (both 0 or both 1), the resulting bit is set to 0.
  5. Assignment: The newly calculated value is assigned to the reference on the left side of the operator.

Truth Table

Left BitRight BitResulting Bit
000
011
101
110

Step-by-Step Evaluation

Number Operands

let a = 5;
a ^= 3; 
console.log(a); // Outputs: 6
Under the hood:
  1. 5 is converted to a 32-bit integer: ...0000 0101
  2. 3 is converted to a 32-bit integer: ...0000 0011
  3. The XOR operation is applied vertically:
  00000000000000000000000000000101  (5)
^ 00000000000000000000000000000011  (3)

  00000000000000000000000000000110  (6)
  1. The variable a is reassigned the value 6.

BigInt Operands

let b = 5n;
b ^= 3n;
console.log(b); // Outputs: 6n
Because these are 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 or BigInts, 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.
let x = 5;
x ^= "3";       // String "3" coerces to Number 3. Result: 6
x ^= null;      // null coerces to Number 0. Result: 6 (6 ^ 0 = 6)
x ^= undefined; // undefined coerces to NaN, which becomes 0. Result: 6
x ^= 2.99;      // 2.99 is truncated to 2. Result: 4 (6 ^ 2 = 4)
Master JavaScript with Deep Grasping Methodology!Learn More