> ## 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.

# JavaScript Bitwise XOR Assignment

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

```javascript theme={"dark"}
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 Bit | Right Bit | Resulting Bit |
| :------: | :-------: | :-----------: |
|    `0`   |    `0`    |      `0`      |
|    `0`   |    `1`    |      `1`      |
|    `1`   |    `0`    |      `1`      |
|    `1`   |    `1`    |      `0`      |

## Step-by-Step Evaluation

### Number Operands

```javascript theme={"dark"}
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:

```text theme={"dark"}
  00000000000000000000000000000101  (5)
^ 00000000000000000000000000000011  (3)

  00000000000000000000000000000110  (6)
```

4. The variable `a` is reassigned the value `6`.

### BigInt Operands

```javascript theme={"dark"}
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 `BigInt`s, 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.

```javascript theme={"dark"}
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)
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor JavaScript Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
