Math.floor()

floor(x)
Returns: number · Added in vES1 · Updated March 13, 2026 · Math
math floor rounding

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)

Parameters

ParameterTypeDescription
xnumberThe 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.

Working with integers

Math.floor(42);    // 42
Math.floor(0);     // 0
Math.floor(-42);   // -42

When given an integer, floor() returns it unchanged.

Handling edge cases

Math.floor(Infinity);  // Infinity
Math.floor(-Infinity); // -Infinity
Math.floor(NaN);       // NaN
Math.floor(0.9999999999999999); // 1 (floating-point precision)

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

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

Practical use: Array chunking

function chunkArray(arr, size) {
  const chunks = [Global_Objects::eval];
  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]]

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

See Also