Remainder in JavaScript

having trouble with the remainder operator concept

i dont get it… start from the beginning

example: var remainder = 11 % 3;

what’s going on here?

please give a few more examples using the remainder operator and explain

kindly explain so its clear : )

thank you!!

In long division we have four parts to the overall expression.

Dividend   =>  the number being divided
Divisor    =>  the number being divided by
Quotient   =>  the whole number result
Remainder  =>  the non-divisible amount

Take 11 over 3 from your example…

11 / 3     =>  3.66..

but when we take 0.66.. and resolve it to a rational number (all repeating decimals are rational numbers) we get 2/3.

Now multiply 2/3 by the divisor, 3, and we get 2. Thus, 11 modulo 3 is 2.

11 % 3     =>  2

Now consider the whole number quotient, 3,

3 * 3      =>  9
11 - 9     =>  2

Again, remainder of 2.

We can use the remainder as a signal of non-divisibility. If the remainder is not zero, then the dividend cannot be divided evenly by the divisor. We now know with certainty that 3 does not divide into 11. There will always be a remainder.

From what we’ve covered above we have a simple formula…

D - qd = R

D is the Dividend; d is the divisor; q is the quotient; and, R is the remainder. This holds for integers or floats though in JavaScript, float remainders are not supported. We must look at this from the integer perspective.

1 Like