Math.floor()
Rounds a number down to the largest integer less than or equal to it.
Math.floor(x)- Added in JavaScript ES1
Parameters
| Parameter | Type | Description |
|---|---|---|
$x | number | A number to round down. Non-numbers are coerced first. |
Return value
numberThe largest integer less than or equal to x. Equivalent to -Math.ceil(-x).
Math.floor() rounds toward negative infinity. For positive numbers that looks like chopping off the decimals — 5.95 becomes 5 — but for negatives it moves away from zero: Math.floor(-1.5) is -2, and Math.floor(-5.05) is -6.
When to reach for it
Its signature use is turning Math.random() into an integer index: Math.floor(Math.random() * arr.length) gives a uniform value in range, because Math.random() never reaches 1. It is also the correct tool for "how many whole units fit" — full minutes in a duration, complete rows in a grid, integer division of two positive numbers.
What trips people up
The confusion is almost always floor versus trunc on negative numbers. They are identical for positive input, which is why the bug hides until a negative value shows up. If you meant "remove the fractional part", you meant Math.trunc(). If you meant "round down on a number line", you meant Math.floor().
People also reach for ~~x or x | 0 as a fast floor. Those are truncation, not flooring, and they coerce to a 32-bit integer, so they silently break for negatives and for values above 231−1. Math.floor() has no such limit.
Like the other rounding methods, Math.floor(-0) preserves -0.
Example
When to use it
- Picking a random array index from Math.random()
- Splitting a duration in seconds into whole minutes and a remainder
- Computing which page or bucket an index falls into
- Converting a positive float to a whole count of units
Frequently asked questions
What is the difference between Math.floor() and Math.trunc()?
They differ only on negative numbers: floor rounds toward negative infinity (-1.5 becomes -2) while trunc drops the fraction toward zero (-1.5 becomes -1). For non-negative values they return the same result.
How do I get a random integer in JavaScript?
Use Math.floor(Math.random() * n) for an integer from 0 to n-1. Floor is correct because Math.random() returns a value in [0, 1), so the product never reaches n.
Is ~~x or x | 0 a faster Math.floor()?
No — they truncate rather than floor, so they give the wrong answer for negative numbers, and they coerce to a 32-bit integer, so they break for values beyond about 2.1 billion.
More examples
Splitting a duration into parts
Each floor takes the whole units at one scale and the remainder feeds the next. Floor is right here rather than round, since 3599 seconds is 0 hours, not 1.
function format(totalSeconds) {
const h = Math.floor(totalSeconds / 3600);
const m = Math.floor((totalSeconds % 3600) / 60);
const s = totalSeconds % 60;
return [h, m, s].map((n) => String(n).padStart(2, "0")).join(":");
}
console.log(format(3725)); // "01:02:05"
console.log(format(59)); // "00:00:59"
console.log(format(86399));// "23:59:59"Why bitwise shortcuts are not a floor
For 7.9 all three agree. For -7.9 the bitwise forms give -7 (truncation) while floor gives -8. Above 2^31-1 the bitwise forms wrap to a negative number entirely, because they coerce to a signed 32-bit integer. Math.floor has neither problem.
const cases = [7.9, -7.9, 2147483648.5];
for (const n of cases) {
console.log(n, "floor:", Math.floor(n), "~~:", ~~n, "| 0:", n | 0);
}
Discussion