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

# TypeScript Remainder Assignment

The `%=` (remainder assignment) operator divides the value of the left operand (dividend) by the value of the right operand (divisor) and assigns the resulting remainder to the left operand. As a compound assignment operator, it evaluates the left-hand side reference only once, distinguishing it from a standard assignment combined with a modulo operation.

```typescript theme={"dark"}
x %= y;
// Conceptually similar to x = x % y, but the reference for 'x' is evaluated only once.
```

## Mechanics

1. **Evaluation:** The left operand is evaluated first to resolve its reference and retrieve its value. *Then*, the right operand is evaluated.
2. **Calculation:** The JavaScript engine performs a truncated division (rounding towards zero) of the left operand's value by the right operand's value to determine the remainder.
3. **Sign Preservation:** The sign of the returned remainder strictly matches the sign of the dividend (the left operand), regardless of the sign of the divisor.
4. **Assignment:** The computed remainder is written back to the resolved memory location of the left operand.

## TypeScript Type Constraints

TypeScript enforces strict type checking on the `%=` operator:

* **Valid Types:** Both operands must resolve to `number` or `bigint`.
* **Type Homogeneity:** You cannot mix `number` and `bigint` operands. Doing so results in a `ts(2365)` compiler error.
* **Mutability:** The left operand must be a valid l-value (a mutable reference, such as a variable declared with `let` or a writable object property). It cannot be a `const`.

## Syntax and Behavior Examples

**Standard Numeric Evaluation:**

```typescript theme={"dark"}
let a: number = 10;
a %= 3; 
// a is now 1 (10 / 3 = 3 with a remainder of 1)
```

**Sign Preservation (Negative Dividend):**

```typescript theme={"dark"}
let b: number = -10;
b %= 3; 
// b is now -1 (Sign matches the left operand)

let c: number = 10;
c %= -3;
// c is now 1 (Sign of the right operand is ignored)
```

**Single Evaluation of Left Operand:**
Because the left operand is evaluated only once, `%=` behaves differently than `x = x % y` when the left operand contains side effects.

```typescript theme={"dark"}
let arr: number[] = [10, 20, 30];
let i: number = 0;

arr[i++] %= 3; 
// i is evaluated and incremented only once. 
// arr[0] becomes 1, and i becomes 1.
// If written as arr[i++] = arr[i++] % 3, i would be incremented twice.
```

**BigInt Evaluation:**

```typescript theme={"dark"}
let d: bigint = 10n;
d %= 3n;
// d is now 1n
```

**TypeScript Compiler Errors:**

```typescript theme={"dark"}
let e: number = 10;
e %= "3"; 
// Error ts(2363): The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.

let f: bigint = 10n;
f %= 3; 
// Error ts(2365): Operator '%=' cannot be applied to types 'bigint' and 'number'.

const g: number = 10;
g %= 3; 
// Error ts(2588): Cannot assign to 'g' because it is a constant.
```

## Edge Cases (IEEE 754 Floating Point)

Because TypeScript `number` types are double-precision 64-bit floats, the operator handles specific edge cases according to the IEEE 754 standard:

* If the dividend is `Infinity` or `-Infinity`, the result is `NaN`.
* If the divisor is `0` or `-0`, the result is `NaN`.
* If the dividend is `0` and the divisor is non-zero, the result is `0`.

```typescript theme={"dark"}
let h: number = Infinity;
h %= 5; 
// h is now NaN

let j: number = 10;
j %= 0; 
// j is now NaN
```

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