Math.floor()
floor(x) The Math.floor() method returns the largest integer less than or equal to a number. It’s one of the most commonly used rounding methods in JavaScript and has been available since ES1.
Syntax
Math.floor(x)
Math.floor() takes a single numeric argument and returns the greatest integer that does not exceed that value. It is a static method on the Math object — you call it as Math.floor() rather than on a number instance, and it works with any value that can be coerced to a number.
Parameters
| Parameter | Type | Description |
|---|---|---|
| x | number | The number to floor |
Return Value
- The largest integer less than or equal to x
Examples
Basic usage
Math.floor(4.8); // 4
Math.floor(4.2); // 4
Math.floor(4.0); // 4
Math.floor(-4.8); // -5
Math.floor(-4.2); // -5
Notice the behavior with negative numbers: floor(-4.8) returns -5 because -5 is the largest integer that is less than or equal to -4.8. This is the key difference from simply truncating — floor() respects the number line, not just the decimal point.
When the argument is already an integer, floor() returns it unchanged. This means you can call floor() on values that may or may not have a fractional part without special-casing the integer case.
Working with integers
Math.floor(42); // 42
Math.floor(0); // 0
Math.floor(-42); // -42
When given an integer, floor() returns it unchanged.
Edge cases exist for values that are at the boundaries of the number system. Infinity, -Infinity, and NaN pass through floor() unchanged — Infinity stays Infinity, and NaN stays NaN. Floating-point precision can also produce surprising results with values very close to integers.
Handling edge cases
Math.floor(Infinity); // Infinity
Math.floor(-Infinity); // -Infinity
Math.floor(NaN); // NaN
Math.floor(0.9999999999999999); // 1 (floating-point precision)
The most direct use of floor() is removing the fractional part of a positive number. For dollar amounts, pixel coordinates, or any value where the whole-number component is what matters, floor() gives you the integer part without rounding up. This is simpler than string manipulation and works inline in arithmetic expressions.
Practical use: Truncating decimals
If you need to remove everything after the decimal point, floor() works for positive numbers:
const price = 29.99;
const wholeDollars = Math.floor(price);
console.log(wholeDollars); // 29
floor() also underpins pagination logic. Page boundaries are calculated by multiplying the page index by the page size, and floor() naturally falls out of integer division when computing page counts or offsets. Combined with Math.ceil() for the total page count, it gives you a complete pagination toolkit.
Practical use: Pagination
const pageSize = 10;
const totalItems = 57;
const totalPages = Math.ceil(totalItems / pageSize);
function getPageStart(page) {
return (page - 1) * pageSize + 1;
}
function getPageEnd(page) {
return Math.min(page * pageSize, totalItems);
}
getPageStart(1); // 1
getPageEnd(1); // 10
getPageEnd(6); // 57
Array chunking is another common pattern where the loop increment and index arithmetic rely on integer steps. While floor() is not explicitly called here, the loop counter i advances by size each iteration, producing the same effect as i = Math.floor(i + size). The pattern is worth recognizing because many chunking implementations use Math.floor(arr.length / size) to pre-compute the number of chunks.
Practical use: Array chunking
function chunkArray(arr, size) {
const chunks = [];
for (let i = 0; i < arr.length; i += size) {
chunks.push(arr.slice(i, i + size));
}
return chunks;
}
const items = [1, 2, 3, 4, 5, 6, 7, 8, 9];
chunkArray(items, 3); // [[1,2,3], [4,5,6], [7,8,9]]
chunkArray(items, 4); // [[1,2,3,4], [5,6,7,8], [9]]
The distinction between floor() and trunc() only matters with negative numbers, but it is a common source of bugs. When you write code that may receive negative inputs — for example, a function that offsets coordinates — the choice between “always round down” and “chop off the decimal” determines whether you get the right cell index.
Key Behaviors
- Always rounds down (toward negative infinity)
- For negative numbers, this means rounding away from zero
- Does not add floating-point precision artifacts like some rounding methods
- Use
Math.trunc()when you want to simply remove fractional digits regardless of sign
floor() vs trunc() for negative numbers
Math.floor() and Math.trunc() behave identically for positive numbers but differ for negatives. trunc() removes the fractional part (rounds toward zero), while floor() rounds toward negative infinity:
Math.floor(4.7); // 4 — same
Math.trunc(4.7); // 4 — same
Math.floor(-4.7); // -5 — rounds away from zero
Math.trunc(-4.7); // -4 — truncates toward zero
Use floor() when you need “the integer part, never exceeding the value.” Use trunc() when you want “drop the decimal, regardless of sign.” For non-negative numbers, the choice is interchangeable.
Generating random integers
The most common use of Math.floor() in everyday code is generating random integers in a range:
// Random integer from min (inclusive) to max (exclusive)
function randInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
randInt(1, 7); // 1, 2, 3, 4, 5, or 6 — like a die roll
randInt(0, 100); // 0–99
Math.random() returns a float in [0, 1). Multiplying by the range and flooring gives a uniform integer in [0, range). Adding the minimum offset shifts the interval to [min, max), which is the standard pattern for dice rolls, random indices, and any uniform integer sampling.
Very close-to-integer floats can produce surprising results due to IEEE 754 representation, so if precise integer rounding matters, validate inputs or use Number.isInteger() to check before flooring.
Floating-point precision edge cases
Very close-to-integer floats can produce surprising results due to IEEE 754 representation:
Math.floor(0.9999999999999999); // 1 (not 0 — rounds to 1.0 in float64)
Math.floor(1.0000000000000002); // 1 (very close to 1, still floors to 1)
These cases are rare in practice but appear when accumulating floating-point arithmetic. If precise integer rounding matters, validate inputs or use Number.isInteger() to check before flooring.
When floor() is the right choice
Math.floor() is the best fit when you need the greatest integer that does not exceed a value. That matters in layout calculations, page indexing, and any place where crossing a boundary should wait until the number truly reaches it. It is also a good match for random-number helpers because the output of Math.random() is a fraction that still needs to be mapped to an integer range.
The sign of the input is the important detail to remember. For positive numbers, flooring looks like simple decimal removal. For negative numbers, it moves one step farther from zero, which makes it different from Math.trunc(). That difference is often the reason one helper works and the other does not.
When to Use floor()
Math.floor() is the right choice whenever you want to round down in a way that respects the number line rather than just dropping decimals. That makes it a good fit for pagination, random integer generation, indexing, and any calculation where the lower whole number is the one you want. For negative values, remember that “round down” means moving farther from zero, which is why floor(-4.2) becomes -5.
See Also
- Math.ceil() — Round up
- Math.round() — Round to nearest
- Math.trunc() — Remove fractional digits