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

The bitwise AND assignment (`&=`) operator performs a bitwise AND operation on two operands and assigns the resulting value to the left operand. For `Number` operands, it evaluates their 32-bit binary representations, applies a logical AND to each corresponding pair of bits, and mutates the left operand with the final 32-bit integer result. For `BigInt` operands, it performs the bitwise AND operation across the arbitrary precision of the operands and assigns the resulting `BigInt`.

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

While often conceptualized as `x = x & y`, the compound assignment operator `&=` evaluates the left-hand side expression only *once*. This distinction is critical when the left operand contains side effects, such as property accessors (getters/setters) or complex expressions. For example, in `arr[i++] &= y`, the index `i` is incremented only once, whereas `arr[i++] = arr[i++] & y` would evaluate and increment `i` twice.

## Execution Mechanics

When the `&=` operator is evaluated, the JavaScript engine performs the following sequence of operations depending on the operand types:

1. **Type Evaluation and Coercion**:
   * If both operands are of type `Number` (or can be coerced to `Number`), they are implicitly converted to 32-bit signed integers using two's complement representation via the internal `ToInt32` abstract operation. Fractional values are truncated, and non-numeric values (e.g., `null`, `undefined`, `NaN`) are coerced to `0`.
   * If both operands are of type `BigInt`, no 32-bit truncation occurs. The operation is performed using the arbitrary-precision two's complement representation of the `BigInt` values.
   * If one operand is a `BigInt` and the other is a `Number`, the engine throws a `TypeError` (e.g., `Cannot mix BigInt and other types`).
2. **Bitwise Evaluation**: The engine compares the operands bit by bit. A bit in the resulting binary sequence is set to `1` if and only if the corresponding bits in *both* operands are `1`. If either bit is `0`, the resulting bit is `0`.
3. **Assignment**: The newly calculated value (`Number` or `BigInt`) is assigned back to the memory space of the left operand.

## Step-by-Step Evaluation Example (`Number`)

```javascript theme={"dark"}
let a = 14; 
let b = 9;  

a &= b; 

console.log(a); // Output: 8
```

**Binary Breakdown:**

* `a` (14) is represented in 32-bit binary as: `...0000 1110`
* `b` (9) is represented in 32-bit binary as:  `...0000 1001`

The bitwise AND operation aligns the bits:

```text theme={"dark"}
  ...0000 1110  (14)
& ...0000 1001  (9)

  ...0000 1000  (8)
```

The resulting binary `1000` evaluates to the base-10 integer `8`, which is then assigned to `a`.

## `BigInt` Behavior

When using `BigInt` operands, the bitwise AND operation scales to the precision of the values without 32-bit truncation:

```javascript theme={"dark"}
let bigX = 0b111100001111000011110000111100001111n;
let bigY = 0b101010101010101010101010101010101010n;

bigX &= bigY;

console.log(bigX); // Output: 43118103050n (0b101000001010000010100000101000001010n)
```

Attempting to mix `BigInt` and `Number` results in a `TypeError`:

```javascript theme={"dark"}
let mixed = 10n;
mixed &= 5; // TypeError: Cannot mix BigInt and other types, use explicit conversions
```

## Coercion Behavior (`Number` Context)

Because the operator forces a `ToInt32` conversion for non-`BigInt` types, applying `&=` to non-integer types results in strict truncation and type casting before the bitwise operation occurs:

```javascript theme={"dark"}
let x = 5.99; // Truncated to 5 (...0101)
x &= 3;       // 3 is (...0011)
// 5 & 3 evaluates to 1 (...0001)
console.log(x); // Output: 1

let y = "12"; // Coerced to number 12 (...1100)
y &= 10;      // 10 is (...1010)
// 12 & 10 evaluates to 8 (...1000)
console.log(y); // Output: 8

let z = NaN;  // Coerced to 0
z &= 5;       
console.log(z); // Output: 0
```

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