JavaScript gives you several ways to round numbers, but not all of them round the same way. If you want to drop the decimal and keep the lower whole number, you use Math.floor().
Table of Content
It works well when you handle prices, time, or indexes where rounding up can break logic. In this article, you will see how Math.floor()
works in JavaScript and where to use it.
Understand the Math.floor() in JavaScript
Math.floor()
is a method that rounds a number down to the nearest whole number. It does not care how close the decimal is to the next number. It always returns the lower number.
Here is the syntax:
Math.floor(number)
It takes one argument. That can be any number (positive, negative, float, or integer). The result is the largest integer less than or equal to the input.
This function always returns a smaller or equal whole number. It does not look at the decimal part. Even if the decimal is 0.999, Math.floor()
still moves the number down.
Here is a quick example:
Math.floor(4.9) // returns 4
Math.floor(4.1) // also returns 4
Math.floor(-4.1) // returns -5
Here are the use cases:
- Create random whole numbers from floats
- Remove decimals from user input or calculations
- Round down to set price points or discounts
- Get whole-number indexes for pagination
- Work with time values like hours, minutes, or dates
- Avoid rounding up by mistake in loops or counters
Examples of JavaScript Math.floor()
Let us look at some real use cases and how Math.floor()
behaves in each.
Round positive decimal numbers:
Math.floor(7.8) // returns 7
Math.floor(2.3) // returns 2
The decimal part gets dropped. The number moves down.
Round negative decimal bumbers:
Math.floor(-3.2) // returns -4
Math.floor(-5.9) // returns -6
It still rounds down. So for negative numbers, the result becomes smaller.
Create random integers with Math.random():
let randomInt = Math.floor(Math.random() * 10); // 0 to 9
This is a common way to pick a random item from a list or shuffle data.
Handle price calculations or discounts:
let fullPrice = 19.99;
let dealPrice = Math.floor(fullPrice); // returns 19
Work with time, pagination, or indexing:
let minutes = 126;
let hours = Math.floor(minutes / 60); // returns 2
You also use it to pick list items safely:
let page = Math.floor(currentIndex / itemsPerPage);
Browser Support for Math.floor() in JavaScript
You can use Math.floor()
in all major browsers. It works in:
- Google Chrome
- Mozilla Firefox
- Microsoft Edge
- Safari
- Opera
- Internet Explorer (version 6 and above)
It has been supported for a long time, even in older versions. You do not need a fallback or polyfill. That means you can use Math.floor()
without any worries about browser issues.
Wrapping Up
In this article, you learned how JavaScript Math.floor()
works and where you can use it. You saw how it always rounds numbers down and how it handles both positive and negative values. You also looked at real examples like price rounding, time logic, and random numbers.
Here is a quick recap:
Math.floor()
returns the largest whole number less than or equal to the input- It drops the decimal without rounding up
- It works with positive and negative numbers
- You can use it for price logic, pagination, or time math
- It has full browser support and needs no fallback
Thank you for reading. Click here to see more articles about JavaScript built-in functions.
FAQs
How to use Math.floor in JavaScript?
Math.floor(4.7); // returns 4
Math.floor(-4.7); // returns -5
You can use it with Math.random() to get random integers:
let random = Math.floor(Math.random() * 10); // returns 0 to 9
What does the Math.floor() method do?
Math.floor(5.9); // returns 5
Math.floor(-5.1); // returns -6
Does Math.floor() Work on Strings?
Math.floor("7.8"); // returns 7
Math.floor("abc"); // returns NaN
If the string cannot convert to a number, the result is NaN.
What If Input Is Already an Integer?
Math.floor(6); // returns 6
Math.floor(-2); // returns -2
Can You Use It with BigInt?
Math.floor(10n); // TypeError: Cannot convert a BigInt value to a number
You must use regular numbers with Math.floor(). If you work with BigInt, use other logic to round manually.