think of it in a different light. Floor will always go down to the last integer.The float 3.8 will be dropped down to 3. Round is just that 3.8 will round up to 4. 3.3 will round down to 3. Answer from Jacob Mishkin on teamtreehouse.com
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › floor
Math.floor() - JavaScript | MDN
The Math.floor() static method always rounds down and returns the largest integer less than or equal to a given number.
Discussions

optimization - Why is Javascript's Math.floor the slowest way to calculate floor in Javascript? - Stack Overflow
This seems pretty shocking as I ... Javascript in today's modern browsers would be some pretty smart people. Does floor do something important that the other methods fail to do? Is there any reason to use it? ... ~~n, n|n and n&n does not produce the same result as Math.fl... More on stackoverflow.com
🌐 stackoverflow.com
Why not always use Math.round instead of Math.floor?

Well, they are two different functions, with two different uses. Math.floor() always rounds down to the nearest integer, while Math.round() will round up or down depending on what side of .5 the number falls on. So, the basic answer is that you use which one gets the result you expect.

When it comes to generating random numbers though, Math.floor() has a more even distribution than Math.round(). If you want to generate a random number between 0 and 2, take the following examples:

Math.floor(Math.random() * 3). Here, 0-0.999999 will give you 0, 1.0 to 1.999999 will give you 1, and 2.0 to 2.999999 will give you 2. Every number has a 33% chance of being the result.

Math.round(Math.random() * 2). Here, 0-0.499999 will give you 0, 0.5 to 1.499999 will give you 1, and 1.5 to 1.999999 will give you 2. Note that the range of numbers that lead to a 1 is twice as big as those that lead to 0 or 1. That is 25% chance of 0, 50% chance of 1, and 25% chance of 2.

More on reddit.com
🌐 r/javascript
13
1
January 8, 2016
Math.floor vs Math.round vs parseInt vs Bitwise jsPerf
What I find interesting is how fast the bitwise stuff is--Crockford said something about not using them in The Good Parts™ because they are so slow. More on reddit.com
🌐 r/javascript
31
21
July 18, 2012
Using Math.floor and modulus for pagination, will it always be correct?
You need Math.ceil() 239/30 = 7.96, so you have more than 7 pages so you should have a partially filled 8th page. Math.ceil(239/30) === 8 More on reddit.com
🌐 r/learnjavascript
2
2
February 5, 2016
🌐
W3Schools
w3schools.com › jsref › jsref_floor.asp
JavaScript Math floor() Method
The Math.floor() method rounds a number DOWN to the nearest integer. The Math.abs() Method The Math.ceil() Method The Math.floor() Method The Math.round() Method The Math.fround() Method The Math.f16round() Method The Math.trunc() Method ...
🌐
Programiz
programiz.com › javascript › library › math › floor
JavaScript Math floor()
Returns the largest integer less than or equal to a given number. Returns 0 for null. ... var num = Math.floor(1.8645); console.log(num); // 1 var num = Math.floor(-0.456); console.log(num); // -1 ... // Returns NaN for non-numeric types var num = Math.floor("JavaScript"); console.log(num); ...
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › Math › floor
JavaScript Math floor() - Round Down Value | Vultr Docs
November 29, 2024 - The Math.floor() function in JavaScript is a crucial tool for rounding down numerical values to the nearest whole number.
🌐
Flexiple
flexiple.com › javascript › javascript-math-floor
Javascript Math.floor() - uses and limitations - Flexiple
March 14, 2022 - Learn how to use Javascript's Math.floor() function and understand its limitations. Get insights on rounding numbers down efficiently and accurately.
🌐
TutorialsPoint
tutorialspoint.com › javascript › math_floor.htm
JavaScript Math.floor() Method
The Math.floor() method in JavaScript accepts a numeric value as an argument, rounds down and returns the largest integer less than or equal to the provided number. For instance, if we provide "5.6" as an argument to this method, it returns "5" as a
Find elsewhere
🌐
Math.js
mathjs.org › docs › reference › functions › floor.html
math.js | an extensive math library for JavaScript and Node.js
math.floor(c, 1) // returns Complex 3.2 -2.8i const unit = math.unit('3.241 cm') const cm = math.unit('cm') const mm = math.unit('mm') math.floor(unit, 1, cm) // returns Unit 3.2 cm ...
Top answer
1 of 2
37

The primary reason Math.floor is slower (where it actually is--in some tests I've done it's faster) is that it involves a function call. Older JavaScript implementations couldn't inline function calls. Newer engines can inline the call, or at least make the property lookup faster, but they still need a guard condition in case you (or some other script) overwrote the Math.floor function. The overhead is minimal though, so there's not much difference in speed.

More importantly though, as was mentioned in several comments, the other methods are not equivalent. They all work by doing bitwise operations. The bitwise operators automatically convert their operands to 32-bit integers by truncating the number. That's fine if the number fits in 32 bits, but JavaScript numbers are 64-bit floats, which could be much larger than 2147483647.

They also give a different result for negative numbers, since converting to integers truncates and Math.floor always rounds down. For example, Math.floor(-2.1) === -3, but (-2.1) | (-2.1) === -2.

If you know you are only dealing with positive numbers less than 2147483648, and you need to squeeze every bit of performance out of your code in older browsers (Make sure it's actually the bottleneck first. It probably isn't.), I would use an even simpler method: x|0. It doesn't evaluate the variable twice, and it works even if x is an expression (just be sure to put it in parentheses so you don't run into precedence issues).

2 of 2
27

It has nothing to do with modern browsers. It has to do with implementing the ECMA standard. You can't just change how a certain function performs even if there is a faster way. It could break existing code.

The Math.Floor has to account for a lot of different scenarios of handling different types. Could they have made different scenarios faster by taking short cuts as you described? Maybe they could, but that might have broken other scenarios. Just because something on the surface looks small, doesn't mean that there isn't an iceberg underneath.

🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-math-floor-method
JavaScript Math floor() Method - GeeksforGeeks
November 7, 2024 - The Math.floor() method in JavaScript is used to round off a number down to the nearest integer, moving towards the lower value.
🌐
W3Schools
w3schools.com › js › js_math.asp
JavaScript Math Object
There are 4 common methods to round a number to an integer: ... Math.floor(4.9); Math.floor(4.7); Math.floor(4.4); Math.floor(4.2); Math.floor(-4.2); Try it Yourself » ... Math.trunc(4.9); Math.trunc(4.7); Math.trunc(4.4); Math.trunc(4.2); Math.trunc(-4.2); Try it Yourself » · Math.sign(x) returns if x is negative, null or positive. ... Math.trunc() and Math.sign() were added to JavaScript 2015 - ES6.
🌐
HCL Software
help.hcl-software.com › dom_designer › 9.0.1 › reference › r_wpdr_standard_math_floor_r.html
floor (JavaScript)
Rounds a number to an integer by truncating the fraction. Math (JavaScript) floor(value:double) : double · Special cases are as follows: Rounding NaN results in NaN. Rounding +Infinity results in positive infinity. Rounding -Infinity results in negative infinity.
🌐
O'Reilly
oreilly.com › library › view › javascript-the-definitive › 0596101996 › re110.html
Math.floor( ): round a number down — ECMAScript v1 - JavaScript: The Definitive Guide, 5th Edition [Book]
August 17, 2006 - JavaScript from Flash23.4.5. Example: Flash to JavaScript, and Back to Flash23.5. Scripting Flash 8 ... Any numeric value or expression. The closest integer less than or equal to x. Math.floor( ) computes the floor function; in other words, ...
Author   David Flanagan
Published   2006
Pages   1018
🌐
JavaScript in Plain English
javascript.plainenglish.io › math-ceil-math-round-and-math-floor-in-your-javascript-7142fde2c56d
Math.ceil, Math.round and Math.floor in Your JavaScript | by Piero Borrelli | JavaScript in Plain English
September 9, 2020 - Luckily for us, a language like JavaScript gives us tools like the Math object to deal with numbers and their operations. When it comes to rounding numbers, Math helps you with three main functions: Math.ceil, Math.floorand Math.round. This method rounds up the only passed in value to the nearest greater integer.
🌐
FlatCoding
flatcoding.com › home › javascript math floor: how it works with examples
JavaScript Math floor: How It Works with Examples - FlatCoding
June 25, 2025 - JavaScript Math.floor() rounds a number down to the nearest integer. Click here to see how it works with examples.
🌐
W3Schools
w3schools.com › js › js_random.asp
JavaScript Random
Math.random() * 10 gives a range from 0 up to but not including 10. Example possible results: 0.0, 3.5, 9.99, etc. Math.floor() rounds a number down to the nearest whole integer:
🌐
Medium
ludmilakorchnoy.medium.com › math-floor-and-math-random-in-javascript-ae63e3bfb91f
Math.floor() and Math.random() in JavaScript | by Ludmila Korchnoy | Medium
January 21, 2021 - Math.floor() and Math.random() in JavaScript Math.floor() function returns the largest integer less than or equal to a given number. For instance: console.log(Math.floor(5.95)); // expected output …
🌐
Reddit
reddit.com › r/javascript › why not always use math.round instead of math.floor?
r/javascript on Reddit: Why not always use Math.round instead of Math.floor?
January 8, 2016 -

When I read through the code of colleagues and public repos, I see Math.floor used like 20x more often than Math.round.

But why? Isn't Math.round more accurate than Math.floor? Shouldn't it be the other way around (using Math.round more often than Math.floor)?

Is Math.floor so much faster than Math.round or am I missing something?

Edit

I am aware that those two do different things. My point is that in my experience, Math.floor is much too often used, when Math.round would simply be more accurate.

Top answer
1 of 6
9

Well, they are two different functions, with two different uses. Math.floor() always rounds down to the nearest integer, while Math.round() will round up or down depending on what side of .5 the number falls on. So, the basic answer is that you use which one gets the result you expect.

When it comes to generating random numbers though, Math.floor() has a more even distribution than Math.round(). If you want to generate a random number between 0 and 2, take the following examples:

Math.floor(Math.random() * 3). Here, 0-0.999999 will give you 0, 1.0 to 1.999999 will give you 1, and 2.0 to 2.999999 will give you 2. Every number has a 33% chance of being the result.

Math.round(Math.random() * 2). Here, 0-0.499999 will give you 0, 0.5 to 1.499999 will give you 1, and 1.5 to 1.999999 will give you 2. Note that the range of numbers that lead to a 1 is twice as big as those that lead to 0 or 1. That is 25% chance of 0, 50% chance of 1, and 25% chance of 2.

2 of 6
3

Math.floor - You have a rating system of stars, and you aren't breaking them up into half stars. You do a query to get all the votes and the math comes back to 4.7 stars. You would use Math.floor here so that you display 4 stars.

 

Math.ceil - You have a slider module that displays 3 slides at a time. This module contains 19 slides. 19/3 = 6.33. If you were to floor or round here you would end up with 6. But to make sure that all 19 slides are shown, you need 7 containers, so you use Math.ceil.

 

Math.round - Anytime you need the closest number without worrying about anything like the above scenarios.