JavaScript %= Remainder assignment Operator
The remainder assignment operator (%=) replaces a variable with the remainder of dividing it by a value.
Description
The remainder assignment operator %= divides the variable by the right operand and assigns the remainder. It's handy for wrapping an index back to the start.
The result keeps the sign of the variable, so a negative value stays negative: let n = -7; n %= 3 leaves -1. To wrap an index that might go below zero, such as when moving backwards through slides, add the length before taking the remainder: i = (i - 1 + length) % length.
Compound assignment operators are evaluated from right to left like plain assignment, and the expression itself returns the new value, although using that value inside a larger expression usually makes code harder to read. The left side must be something that can be assigned to: a variable declared with let or var, an object property or an array element. Using one on a const throws a TypeError.
Properties
- Operator
%=- Category
- Assignment
- Precedence
- 2 (1 is lowest, 18 is highest)
- Associativity
- right-to-left
Syntax
x %= yExample
Browser Support
The following information will show you the current browser support for the %= operator. Hover over a browser icon to see the version that first introduced support for this operator.
Desktop





Tablets & Mobile





