TL ; DR:
The Math.floor() function returns the largest integer less than or equal to a given number:
console.log(Math.floor(3.9)); // Output: 3
console.log(Math.floor(5.2)); // Output: 5
console.log(Math.floor(7)); // Output: 7
We can use this function if we want to compute the integral part of some division (quotient):
let a = 3;
let b = 7;
console.log(Math.floor(20 / a)); // Output: 6
console.log(Math.floor(b / a)); // Output: 2
Full lesson:
Remember how we first learn about the division of two integer numbers in primary school?
The quotient is the number of times a division is completed fully, while the remainder is the amount left that doesn't entirely go into the divisor.
Here are some examples:
10 / 2 = quotient 5, remainder 0
15 / 4 = quotient 3, remainder 3
20 / 3 = quotient 6, remainder 2
Floor division
Floor division (//) is a normal division operation except that it returns the integral part of the result (the quotient):
print(10 // 2) # Output: 5
print(15 // 4) # Output: 3
print(20 // 3) # Output: 6
It can also be used with variables:
a = 3
b = 7
print(20 // a) # Output: 6
print(b // a) # Output: 2
Modulo
The modulo operator (%) calculates the remainder of dividing two values:
print(10 % 2) # Output: 0
print(15 % 4) # Output: 3
print(20 % 3) # Output: 2
# Can be used with variables:
a = 2
b = 4
print(b % a) # Output: 0
print(11 % b) # Output: 3
Quotient and remainder
In programming, we combine both these concepts to get the quotient and remainder of some divison:
# Let's divide 26 by 3:
quotient = 26 // 3
remainder = 26 % 3
print(quotient) # Output: 8
print(remainder) # Output: 2
Assignment
Follow the Coding Tutorial and let's practice with quotient and remainder!
Hint
Look at the examples above if you get stuck.
JavaScript has no // operator like Python. Instead, you achieve floor division with Math.floor(a / b) — but the distinction between floor and truncate matters more than beginners expect, especially with negative numbers. JavaScript's % operator also differs from Python's for negatives. Understanding these differences prevents hard-to-trace bugs in pagination, indexing, and algorithm problems.
console.log(10 / 3); // 3.3333... — true division, always float
console.log(Math.floor(10 / 3)); // 3 — floor division, integer quotient
console.log(10 % 3); // 1 — remainder
// For positive numbers they always satisfy:
// Math.floor(a / b) * b + (a % b) === a
// 3 * 3 + 1 === 10 ✓
Math.floor() rounds toward negative infinity (always down). Math.trunc() rounds toward zero. For positive numbers they're identical — for negatives they diverge:
// Positive numbers — same result:
console.log(Math.floor(10 / 3)); // 3
console.log(Math.trunc(10 / 3)); // 3
// Negative numbers — DIFFERENT results:
console.log(Math.floor(-10 / 3)); // -4 (-3.33 floors toward -∞)
console.log(Math.trunc(-10 / 3)); // -3 (-3.33 truncates toward zero)
// Which matches Python's // ?
// Python: -10 // 3 = -4 → Math.floor() matches Python's behavior
JavaScript's % is a remainder operator (sign follows dividend). Python's % is a modulo operator (sign follows divisor). They differ for negatives:
// Positive: identical
console.log(10 % 3); // 1 (same in JS and Python)
// Negative dividend: DIFFERENT
console.log(-10 % 3); // -1 in JavaScript (sign follows -10)
// Python: -10 % 3 = 2 (sign follows 3)
// To get Python-style always-non-negative modulo in JavaScript:
function pyMod(a, b) {
return ((a % b) + b) % b;
}
console.log(pyMod(-10, 3)); // 2 — matches Python's -10 % 3
// (Math.floor(a / b)) * b + (a % b) === a (for a, b > 0)
const a = 26, b = 3;
const quotient = Math.floor(a / b); // 8
const remainder = a % b; // 2
console.log(quotient * b + remainder === a); // true (8*3 + 2 = 26)
// 1. Convert seconds to minutes and seconds:
const totalSeconds = 137;
const minutes = Math.floor(totalSeconds / 60); // 2
const seconds = totalSeconds % 60; // 17
console.log(`${minutes}m ${seconds}s`); // "2m 17s"
// 2. Binary search midpoint (no overflow):
const left = 0, right = 100;
const mid = Math.floor((left + right) / 2); // 50
// 3. Total pages needed (ceiling division):
const totalItems = 95, perPage = 10;
const pages = Math.ceil(totalItems / perPage); // 10
console.log(`${pages} pages`);
// 4. Distribute items into rows:
const items = 29, perRow = 4;
const fullRows = Math.floor(items / perRow); // 7
const leftover = items % perRow; // 1
console.log(`${fullRows} full rows, ${leftover} left over`);
// operator — use Math.floor(a / b) for floor division.Math.floor() rounds toward −∞; Math.trunc() rounds toward zero — they differ for negatives.% returns a negative result when the dividend is negative — unlike Python's %.((a % b) + b) % b to get Python-style always-non-negative modulo.With floor division solid, explore the full set of JavaScript Math methods: Math.ceil() for ceiling division, Math.round() for nearest-integer rounding, and Math.abs() for absolute value. Then apply modulo to practical problems: checking even/odd (n % 2 === 0), circular array indexing (i % arr.length), and FizzBuzz — a classic interview exercise built on %.
AI writes the code. Problem-solving gets you hired. Build the skills you need to land your dream tech job.
Start Coding for FREE