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

The `%` operator in TypeScript is the arithmetic remainder operator. It evaluates to the remainder left over when a dividend (the left operand) is divided by a divisor (the right operand) using truncating division.

```typescript theme={"dark"}
dividend % divisor
```

## TypeScript Type Constraints

TypeScript enforces strict type checking on the `%` operator. Valid operands must be of type `number`, `bigint`, `any`, or a numeric `enum`.

1. **Type Homogeneity:** You cannot mix `number` and `bigint` operands. Doing so results in a compiler error.
2. **No Implicit Coercion:** Unlike loose JavaScript, TypeScript prevents implicit coercion of invalid types (such as strings, booleans, or objects). It throws a `ts(2362)` error if an invalid type is placed on the left-hand side of the operator, and a `ts(2363)` error if an invalid type is placed on the right-hand side.

```typescript theme={"dark"}
enum Config { Offset = 3 }

const a: number = 10 % 3;             // Valid: number % number
const b: bigint = 10n % 3n;           // Valid: bigint % bigint
const c: number = 10 % Config.Offset; // Valid: number % enum
const d: number = 10 % ("3" as any);  // Valid: number % any

const e = 10n % 3;                    // Error ts(2765): Cannot mix 'bigint' and 'number'.
const f = "10" % 3;                   // Error ts(2362): The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
const g = 10 % "3";                   // Error ts(2363): The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
```

## Evaluation Mechanics and Sign Preservation

A critical mechanical detail of the `%` operator is that it is a **remainder** operator, not a strict mathematical modulo. The sign of the returned result always matches the sign of the **dividend** (the left operand), regardless of the sign of the divisor.

The underlying evaluation follows the equation: `r = n - (d * trunc(n / d))`

```typescript theme={"dark"}
// Positive dividend
console.log(10 % 3);   //  1
console.log(10 % -3);  //  1 (Sign of divisor is ignored)

// Negative dividend
console.log(-10 % 3);  // -1 (Sign matches the -10 dividend)
console.log(-10 % -3); // -1
```

## Floating-Point Operands

The `%` operator fully supports floating-point numbers. It does not coerce operands to integers before evaluation.

```typescript theme={"dark"}
console.log(5.5 % 2);    // 1.5
console.log(5.5 % 2.1);  // 1.3000000000000003 (Subject to standard IEEE 754 precision limits)
```

## Edge Cases and Special Values

When dealing with `number` types, the `%` operator adheres to IEEE 754 rules for special values:

* **Division by Zero:** Evaluates to `NaN` (Not-a-Number). It does *not* throw a runtime error or evaluate to `Infinity`.
* **Infinity:** If the dividend is `Infinity` or `-Infinity`, the result is `NaN`. If the divisor is `Infinity`, the result is the dividend.

```typescript theme={"dark"}
console.log(10 % 0);         // NaN
console.log(Infinity % 2);   // NaN
console.log(10 % Infinity);  // 10
```

*Note: When using `bigint`, division by zero (`10n % 0n`) will throw a `RangeError: Division by zero` at runtime, as `bigint` does not have a `NaN` concept.*

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