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

The `|=` (Bitwise OR assignment) operator evaluates the binary representations of its left and right operands, performs a bitwise OR operation (`|`), and assigns the computed result to the left operand.

```javascript theme={"dark"}
x |= y
```

While functionally similar to `x = x | y`, the `|=` operator evaluates the left-hand expression **only once**. This semantic distinction is critical when the left operand contains side effects, such as property accessors or increment/decrement operations.

```javascript theme={"dark"}
let i = 0;
const arr = [10, 20];

// The expression 'i++' is evaluated only once. 
// The reference to 'arr[0]' is resolved, the bitwise OR is applied with 5, 
// and the result is assigned back to 'arr[0]'. 'i' increments to 1.
arr[i++] |= 5; 

// In contrast, the expanded form evaluates 'i++' twice:
// arr[i++] = arr[i++] | 5;
// Due to left-to-right evaluation:
// 1. LHS resolves to arr[0], and 'i' increments to 1.
// 2. RHS reads arr[1] (since 'i' is now 1), and 'i' increments to 2.
// 3. The result of (arr[1] | 5) is assigned to arr[0].
```

## Execution Mechanics

When the JavaScript engine encounters the `|=` operator, it executes the following sequence based on the operand types:

1. **Type Resolution**: The engine determines the numeric type of the operands. If one operand is a `BigInt` and the other is a `Number`, the engine throws a `TypeError`.
2. **Numeric Conversion and Alignment**:
   * **For `Number` operands**: Both operands are implicitly converted to 32-bit signed integers using the internal `ToInt32` abstract operation. Fractional values are truncated (e.g., `5.9` becomes `5`), and non-numeric values are coerced to numbers (or `0` if coercion results in `NaN`). The engine aligns their 32-bit two's complement binary representations.
   * **For `BigInt` operands**: The engine does not truncate to 32 bits. It aligns the arbitrary-precision integers using two's complement representation.
3. **Bitwise Evaluation**: A logical OR is applied to each pair of corresponding bits. The resulting bit is `1` if at least one of the corresponding bits in the operands is `1`. It is `0` only if both bits are `0`.
4. **Assignment**: The resulting integer (either a 32-bit `Number` or a `BigInt`) is assigned back to the left operand.

## Bitwise OR Truth Table

| Bit A (Left) | Bit B (Right) | Result (A \| B) |
| :----------- | :------------ | :-------------- |
| 0            | 0             | **0**           |
| 0            | 1             | **1**           |
| 1            | 0             | **1**           |
| 1            | 1             | **1**           |

## Code Visualization

```javascript theme={"dark"}
// Number evaluation (32-bit ToInt32 conversion)
let a = 10; 
let b = 6;  

a |= b;

//   00000000000000000000000000001010  (10)
// | 00000000000000000000000000000110  (6)
// -------------------------------
//   00000000000000000000000000001110  (14)
console.log(a); // 14

// BigInt evaluation (Arbitrary-precision)
let c = 10n;
let d = 6n;

c |= d; 
console.log(c); // 14n
```

## Coercion and Type Behavior

Because of the mandatory `ToInt32` conversion for standard numbers and the strict type requirements for `BigInt`, using `|=` yields specific structural behaviors:

```javascript theme={"dark"}
let x = 5.99;
x |= 2; 
// 5.99 is truncated to 5 (0101). 2 is (0010). 
// 0101 | 0010 = 0111 (7).
console.log(x); // 7

let y = "12";
y |= 1;
// "12" is coerced to number 12 (1100). 1 is (0001).
// 1100 | 0001 = 1101 (13).
console.log(y); // 13

let z = NaN;
z |= 4;
// NaN is coerced to 0 (0000). 4 is (0100).
// 0000 | 0100 = 0100 (4).
console.log(z); // 4

let big = 10n;
big |= 5; 
// TypeError: Cannot mix BigInt and other types, use explicit conversions
```

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