The Modulo Operator

Remainder with %

The % operator reports what is left over after one number is divided by another.
• 29 % 4 is 1, because four goes into twenty-nine seven times with 1 to spare.
• 58 % 9 is 4, because nine goes into fifty-eight six times with 4 to spare.

What a remainder is good for

• The last digit of a number: 91834 % 10 is 4.
• The last three digits: 47201638 % 1000 is 638.
• Whether a number is odd: 15 % 2 is 1, while 62 % 2 is 0.

Getting the quotient as well

JavaScript has no floor-division operator. Division here never truncates: 87 / 5 is 17.4, not 17, because there is a single number type and nothing is rounded off on your behalf.
Math.floor rounds a number DOWN to a whole number, so Math.floor(87 / 5) is 17. Rounding down and cutting the decimals off land in the same place on a positive number and in different places on a negative one, which is why the two have separate names. Paired with %, Math.floor splits a division into its two useful halves.
QuestionOperatorExample
How many whole groups?Math.floor(a / b)Math.floor(43 / 8) is 5
What is left over?a % b43 % 8 is 3
What is the exact answer?a / b43 / 8 is 5.375
The program below packs cookies into tins of 8 and prints the full tins, the cookies left over, and the exact quotient. Run it, then change it and run it again:
• Set cookies to 88.
• Set cookies to 7, fewer than one tin holds.
• Set perTin to 10, and compare the cookies left over with the last digit of cookies.
let cookies = 86;
let perTin = 8;
console.log("Full tins:", Math.floor(cookies / perTin));
console.log("Cookies left over:", cookies % perTin);
console.log("Exact quotient:", cookies / perTin);
Output
Press Run to see what this program prints.

Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.

Negatives keep the sign of the left operand

A remainder in JavaScript takes its sign from the number being divided, never from the divisor.
• -11 % 3 is -2
• 11 % -3 is 2

Walking through the digits

Two facts do all the work. A remainder by ten pulls off the last digit. Dividing by ten and rounding down leaves a counting number with that last digit gone. Apply them in turn and you can read a number one digit at a time.

Reading a total in mixed units

A tape measure reads 100 inches, and you want that in yards, feet and inches. Math.floor answers how many of a unit fit, % answers what is still unaccounted for, and the leftover becomes the next line of work.
• Yards: Math.floor(100 / 36) is 2, and 100 % 36 leaves 28 inches.
• Feet: Math.floor(28 / 12) is 2, and 28 % 12 leaves 4 inches.
• Inches: 4 are left, and no unit larger than an inch fits inside them. Done.
The program below splits a number of hours into weeks, days, and hours, in the same steps. A week is 168 hours and a day is 24. Run it, then change it and run it again:
• Set total to 168.
• Set total to 23, less than a day.
• Change Math.floor(left / 24) to Math.floor(total / 24), and compare the days with the 7 in a week.
let total = 445;
let weeks = Math.floor(total / 168);
let left = total % 168;
let days = Math.floor(left / 24);
let hours = left % 24;
console.log("Weeks:", weeks);
console.log("Days:", days);
console.log("Hours:", hours);
Output
Press Run to see what this program prints.

Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.