Integer division in JavaScript explained

Updated Sep 22, 2023 โคณ 4 min read

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 quotient and the remainder of a division separately).

๐ŸŽง Debugging Jam

Calling all coders in need of a rhythm boost! Tune in to our 24/7 Lofi Coding Radio on YouTube, and let's code to the beat โ€“ subscribe for the ultimate coding groove!" Let the bug-hunting begin! ๐ŸŽต๐Ÿ’ป๐Ÿš€

24/7 lofi music radio banner, showing a young man working at his computer on a rainy autmn night with hot drink on the desk.

All numbers in JavaScript (except for BigInt numbers) are of type Number, representing floating-point numbers like 25, 12.25, and -3.45.

For instance, the output of the following expression is 1.5:

let result = 9 / 6
// output: 1.5

But if you need to find the quotient of a division without the decimal point and the fraction portion, you need to round it.

Let's see how.

Rounding the quotient in JS integer division

One way to get rid of the fraction is to convert the floating point number to an integer - via Math.floor() and Math.ceil() functions.

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 result = Math.floor(9 / 6)
// output: 1

There's a limitation, though; This approach only works with positive quotients. If the dividend is negative, you might get unexpected results:

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. Obviously, that's not what we want.

As a workaround, 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.

Here's a test with some floating point numbers:

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

Sounds like a good alternative to the first approach.

You might want to use parseInt() to convert the quotient (in floating points) to an integer. However, using the parseInt function to truncate large or small numbers can produce unexpected results because these numbers often contain the "e" character in their string representation (e.g., 6.022e23 for 6.022 ร— 1023).

Bonus tip: You an also add commas to the quotient value to make it more readable if you're dealing with large numbers.

Using the bitwise operator ~~ to truncate numbers

Bitwise operators in JavaScript convert their operand to a 32-bit integer. Some developers use the ~~ operator as a "faster" alternative to rounding functions.

 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

And that's how to use Math.ceil(), Math.floor() or Math.trunc() functions in a JS integer division.

I hope you found this short guide helpful.

Thanks for reading!

Disclaimer: This post may contain affiliate links. I might receive a commission if a purchase is made. However, it doesnโ€™t change the cost youโ€™ll pay.