Integer division in JavaScript explained

Five slices of orange.
Photo by Lucas George Wendt. Source: Pexels

How to do integer division in JavaScript

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).

All numbers in JavaScript 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 you might be interested in the integer part - without a decimal point and the fraction portion following 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 

Math.trunc(): a modern way for integer division in JavaScript

The Math.trunc() cuts off the decimal point and the fraction portion, whether the given number is positive or negative.

Here's a test:

 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!

Author photo

Hey 👋 I'm a software engineer, an author, and an open-source contributor. I enjoy helping people (including myself) decode the complex side of technology. I share my findings on Twitter: @lavary_

If you read this far, you can tweet to the author to show them you care. Tweet a Thanks

In this article:

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