Modulus Assignment Operator
In JavaScript, Modulus Assignment Operator is used find the remainder in the division of the given two operands, and assigns the resulting remainder back to the first operand.
Syntax
%=
symbol is used for modulus assignment operator.
Modulus assignment operator takes two operands, divides (integer division) the value in left operand with that of the right operand, and assigns the remainder back to the left operand.
a %= 5 //equivalent to a = a % 5
a %= b //equivalent to a = a % b
Examples
1. Find the remainder of a
division by 5
operation, and assign the remainder to a
.
var a = 13;
a %= 5;
console.log(a);
2. Find the remainder of a
division by b
operation, and assign the remainder to a
.
var a = 9;
var b = 4;
a %= b;
console.log(a);