✋ Update: This post was originally published on my blog decodingweb.dev, where you can read the latest version for a 💯 user experience. ~reza
In this guide, you’ll learn how to do integer division in JavaScript. The division operator in JavaScript (/
) divides two numbers (dividend and divisor) and returns the quotient as a floating point number (rather than the classic quotient and a remainder).
Numbers in JavaScript (except for BigInt values) are of type number, representing floating-point numbers like 25, 12.25, and -3.45. Thanks to Jon Randy for the correction.
For instance, the output of the following expression is 1.5
:
let result = 9 / 6
// output: 1.5
But you might be interested in the integer part - without a decimal point and the fraction portion following it.
Let's see how we can get it.
Rounding the quotient in JS integer division
The Math.floor()
function always rounds down and returns the largest integer less than (or equal) to the given number. For instance, 1.5
would become 1
.
Let's see an example:
let result = Math.floor(9 / 6)
// output: 1
This approach only works with a positive quotient, though. If the dividend is negative, you might get an unexpected result:
let result = Math.floor(-9 / 6)
// output: -2 (the largest integer *less than* -1.5)
// expected output: -1
The reason is that Math.floor()
rounds down to the first integer number less than -1.5
, and since it's a negative number, the first integer less than -1.5
is -2
.
You can use Math.ceil()
for negative quotients. Unlike Math.floor()
, the Math.ceil()
function always rounds up and returns the smaller integer greater than or equal to a given number.
Let's make a simple function, and try it out with different parameters:
function intDivide(dividend, divisor) {
let quotient = dividend / divisor
// Use Math.ceil if the quotient is negative
if (quotient < 0) {
return Math.ceil(quotient)
}
return Math.floor(quotient)
}
intDivide(9, 6) // output 1
intDivide(-9, 6) // output -1
How about Math.trunc()?
The Math.trunc()
cuts off the decimal point and the fraction portion, whether the given number is positive or negative.
Math.trunc(-1.5) // -1
Math.trunc(-0.5) // -0
Math.trunc(-0) // -0
Math.trunc(0) // 0
Math.trunc(0.75) // 0
Math.trunc(25.65) // 25
Tip: You an also add commas to the quotient value to make it more readable.
Using the bitwise operator ~~
to truncate numbers
Bitwise operations convert the operand to a 32-bit integer. Many use this technique as a "faster" alternative to JS integer division.
The speed difference isn't that noticeable, though. And the performance isn't guaranteed across different browsers.
Please beware that since your number is converted into a 32-bit integer, you should only use it if the given number is within the range of 32-bit integers (-2147483649
< value < 2147483648
). Otherwise, you'll get totally unexpected results:
let value = ~~65.45
// ✅ output: 65
let value = ~~2147483648
// 🚫 output: -2147483648
let value = ~~-2147483649
// 🚫 output: 2147483647
let value = ~~4294967296
// 🚫 output: 0
So which one would you choose? I'd go with Math.trunc()
as it's been designed just for this purpose - Unless there's a good reason to take the other approaches.
I hope you found this short guide helpful.
Thanks for reading!
❤️ You might like:
- Cannot find module error in Node.js (Fixed)
- TypeError: map is not a function in JavaScript in JavaScript (Fixed)
- Add commas to numbers in JavaScript (Explained with examples)
- SyntaxError: Unexpected end of JSON input in JavaScript (Fixed)
- How to fix "ReferenceError: document is not defined" in JavaScript
Top comments (3)
Except those that are of type
BigInt
. Integer division works straight away with those:Thanks you for the correction, Jon! I updated the article :-)
Сongratulations 🥳! Your article hit the top posts for the week - dev.to/fruntend/top-10-posts-for-f...
Keep it up 👍