Skip to main content
The remainder assignment operator (%=) evaluates the remainder of dividing the left operand (dividend) by the right operand (divisor) and assigns the resulting value back to the left operand. It is a compound assignment operator that strictly follows IEEE 754 floating-point arithmetic rules for Number types and algebraic rules for BigInt types.

Syntax and Evaluation Mechanics

While logically similar to x = x % y, the compound assignment operator evaluates the left operand exactly once. This mechanical distinction is critical when the left operand contains side effects, such as property accessors or increment operators.

Technical Mechanics

  1. Evaluation Order: The operator evaluates the left operand, then the right operand.
  2. Sign Preservation: The sign of the resulting remainder always matches the sign of the dividend (the left operand). The sign of the divisor (the right operand) is ignored.
  3. Type Coercion: Before the remainder operation occurs, JavaScript applies the internal ToNumeric abstract operation. Operands are implicitly coerced into either Number or BigInt values. Objects can be coerced into BigInt values (rather than numbers) if their valueOf or [Symbol.toPrimitive] methods return a BigInt.
  4. BigInt Compatibility: The operator works with BigInt values, but both operands must resolve to the same numeric type. Mixing BigInt and Number throws a TypeError.

Evaluation Examples

Standard Evaluation
Sign Preservation (Negative Dividend)
Implicit Type Coercion

Edge Cases

  • Division by Zero:
    • For Number types, dividing by zero assigns NaN to the left operand.
    • For BigInt types, dividing by 0n throws a RangeError: Division by zero.
let f = 5; f %= 0; // f is now NaN let g = 5n; g %= 0n; // Throws RangeError
  • Infinity Divisor: If the right operand is Infinity and the left operand is finite, the left operand remains unchanged.
let j = 5; j %= Infinity; // j is now 5