Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JavaScript · Function

Math.floor

Math.floor() returns the largest integer less than or equal to a number. Unlike truncation, it rounds negative fractional values toward negative infinity.

How it works

Math.floor(x) converts its argument to a number and returns the greatest integer that is less than or equal to it. For negative fractional values, this moves toward negative infinity, so -5.05 becomes -6; integral values, infinities, and signed zero retain their corresponding values.

Syntax

Math.floor(x)

Parameters

NameTypeDescription
xnumberThe numeric value to floor.

Returns

The largest integer less than or equal to x.

Key points

  • It rounds toward negative infinity, not simply toward zero.
  • For 5.95 it produces 5; for -5.05 it produces -6.
  • Its result can be expressed equivalently by negating the ceiling of the negated input.
  • The returned number is the greatest integral value that does not exceed the argument.
  • The function accepts one numeric argument, x.
  • A positive fractional value is reduced to the next lower integer.
  • A negative fractional value becomes the next integer toward negative infinity.

Common mistakes

  • Call it on the built-in Math namespace; it is not an instance method and Math itself is not constructible.
  • Negative infinity remains negative infinity rather than being converted to a finite integer.
  • The function preserves negative zero, which can matter when code distinguishes signed zero.

Example: Floor positive and negative numbers

const values = [5.95, 5.05, -5.05, -0];
const floored = values.map((value) => Math.floor(value));
console.log(floored); // [5, 5, -6, -0]

Maps each input to the greatest integer that does not exceed it.

When to use it

Elapsed-time calculations

Convert a duration to completed whole seconds without counting a partial second.

Random integer selection

Turn a random decimal into an integer index or bounded integer.

Lower-bound bucketing

Map a continuous numeric measurement to the lower discrete bucket.

Interview questions

QWhat does Math.floor do?

Math.floor(x) returns the largest integer less than or equal to x. Consequently, positive fractional values move down toward zero, while negative fractional values move farther from zero toward negative infinity.

Follow-up: How does it differ from truncation for negative values? Math.floor(-5.05) is -6, whereas truncation removes the fraction toward zero and would produce -5.

QWhat are its parameter and return value?

Its signature is Math.floor(x), where x is a number. The returned value is the largest integer smaller than or equal to x, and the operation also handles special numeric values such as infinities and signed zero.

Follow-up: What happens for an integral input? The same number is returned because it already satisfies the floor condition.

QWhen would you use Math.floor in application code?

Math.floor is useful when a calculation must produce an integer that never exceeds a quantity, such as converting elapsed milliseconds to completed seconds or selecting a zero-based random index. It is a static method, so it is accessed through Math.floor().

Follow-up: How would you select an inclusive random integer from min through max? Use Math.floor(Math.random() * (max - min + 1)) + min, assuming valid numeric bounds.

  • Math.ceil()
  • Math.round()
  • Math.trunc()
  • Math.random()

Source: Math.floor() - JavaScript | MDN. Checked 2026-10-15.