Skip to main content

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.

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.
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:
let a: number = 10;
a %= 3; 
// a is now 1 (10 / 3 = 3 with a remainder of 1)
Sign Preservation (Negative Dividend):
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.
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:
let d: bigint = 10n;
d %= 3n;
// d is now 1n
TypeScript Compiler Errors:
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.
let h: number = Infinity;
h %= 5; 
// h is now NaN

let j: number = 10;
j %= 0; 
// j is now NaN
Master TypeScript with Deep Grasping Methodology!Learn More