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

The `^` operator in JavaScript is the **Bitwise XOR (Exclusive OR)** operator. It evaluates two numeric operands at the binary level, returning a `1` in each bit position where the corresponding bits of the operands differ, and a `0` where they are identical.

```javascript theme={"dark"}
operand1 ^ operand2
```

## Execution Mechanics and Coercion

The JavaScript engine evaluates the `^` operator differently depending on whether the operands are standard `Number` types or `BigInt` types.

**Standard `Number` Operands**
Before performing the bitwise operation on standard numbers, the engine applies the `ToInt32` abstract operation to both operands. This enforces the following transformations:

1. **Type Coercion:** Non-numeric types are implicitly converted to numbers. Values that cannot be converted to valid numbers (like `NaN` or `undefined`) are coerced to `0`.
2. **Truncation:** Any fractional components of floating-point numbers are discarded.
3. **32-bit Conversion:** The resulting integers are treated as 32-bit signed integers using two's complement representation.

**`BigInt` Operands**
When both operands are `BigInt` values, the `^` operator evaluates them as arbitrarily large integers. `BigInt` operands **do not** undergo the `ToInt32` abstract operation and are never truncated to 32 bits. The XOR operation is applied across the full binary representation of the arbitrarily large values.

**Mixed Operands**
JavaScript does not allow implicit coercion between `BigInt` and `Number` types for bitwise operations. Mixing them throws a `TypeError`.

## The XOR Truth Table

Regardless of the numeric type, the operator compares the binary representations bit-by-bit according to the following logic:

| Bit A | Bit B | A ^ B |
| :---: | :---: | :---: |
|   0   |   0   |   0   |
|   0   |   1   |   1   |
|   1   |   0   |   1   |
|   1   |   1   |   0   |

## Evaluation Examples

**Standard Number Evaluation**
Consider the expression `5 ^ 3`:

1. The integer `5` is represented in 32-bit binary as: `00000000000000000000000000000101`
2. The integer `3` is represented in 32-bit binary as: `00000000000000000000000000000011`
3. The XOR operation compares them vertically: `00000000000000000000000000000110`
4. The resulting binary translates back to the base-10 integer `6`.

```javascript theme={"dark"}
const a = 5;
const b = 3;

console.log(a ^ b); // Output: 6
```

**BigInt Evaluation**
The operation works identically on `BigInt` values, but without the 32-bit boundary constraints.

```javascript theme={"dark"}
const largeA = 5n;
const largeB = 3n;

console.log(largeA ^ largeB); // Output: 6n
```

## Type Coercion Behavior

Because of the `ToInt32` conversion step for standard numbers, the `^` operator exhibits specific behaviors when encountering non-integer or non-numeric types. Attempting to mix `BigInt` with other types results in an error.

```javascript theme={"dark"}
// Floating-point truncation
5.9 ^ 3.2;       // Output: 6 (Evaluated as 5 ^ 3)

// String coercion
"5" ^ "3";       // Output: 6 (Strings parsed to integers)
"hello" ^ 5;     // Output: 5 (NaN ^ 5 evaluates to 0 ^ 5)

// Boolean coercion
true ^ false;    // Output: 1 (Evaluated as 1 ^ 0)

// Null and Undefined
null ^ 5;        // Output: 5 (Evaluated as 0 ^ 5)
undefined ^ 5;   // Output: 5 (Evaluated as 0 ^ 5)

// Mixed Types (BigInt and Number)
5n ^ 3;          // Throws TypeError: Cannot mix BigInt and other types
```

## Compound Assignment

The Bitwise XOR operator can be combined with the assignment operator (`=`) to apply the operation and assign the result to the left operand in a single step. This works for both `Number` and `BigInt` types.

```javascript theme={"dark"}
let x = 5;
x ^= 3; 
console.log(x); // Output: 6

let y = 5n;
y ^= 3n;
console.log(y); // Output: 6n
```

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