What does Math.floor() do to a number?
Answer
Rounds down to the nearest integer
Answer
Rounds down to the nearest integer
Math.floor() rounds a number down to the nearest integer.
In JavaScript, Math.floor(x) returns the greatest integer less than or equal to x. For example, Math.floor(4.9) returns 4, while Math.floor(4.1) also returns 4. The important detail is that “down” means toward negative infinity, not simply toward zero: Math.floor(-2.3) returns -3.
That behavior distinguishes floor from truncation. Removing the decimal portion would make -2.3 become -2, which is what truncation does, but Math.floor(-2.3) moves to the next lower integer. Math.ceil() performs the opposite operation by moving toward positive infinity, and Math.round() chooses the nearest integer according to JavaScript’s rounding rules.
The name comes from the mathematical floor function, written with floor brackets and used throughout number theory, algebra, and computer science. Math.floor() is often used when converting a random decimal into an index or range, although developers must still account for the fact that Math.random() produces values from zero up to, but not including, one.
Source: Wikipedia · fact-checked Aug. 2026