Integer division in JavaScript explained
Tags: javascript, webdev, beginners
Published: 2023-02-06 15:52:41

> ✋ **Update:** This post was originally published on my blog [decodingweb.dev](https://www.decodingweb.dev), where you can read the [latest version](https://www.decodingweb.dev/js-integer-division) 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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) values) are of type number,  representing floating-point numbers like 25, 12.25, and -3.45. Thanks to Jon Randy for the [correction](https://dev.to/jonrandy/comment/24ip4). 

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

```js
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:

```js
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:

```js
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:

```js
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.

```js
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](https://www.decodingweb.dev/add-commas-to-numbers-in-javascript) 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:

```js
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)](https://www.decodingweb.dev/cannot-find-module-error-in-node-js-fixed)
- [TypeError: map is not a function in JavaScript in JavaScript (Fixed)](https://www.decodingweb.dev/fix-map-is-not-a-function-error-in-javascript)
- [Add commas to numbers in JavaScript (Explained with examples)](https://www.decodingweb.dev/add-commas-to-numbers-in-javascript)
- [SyntaxError: Unexpected end of JSON input in JavaScript (Fixed)](https://www.decodingweb.dev/syntaxerror-unexpected-end-of-json-input-in-javascript)
- [How to fix "ReferenceError: document is not defined" in JavaScript](https://www.decodingweb.dev/how-to-solve-referenceerror-document-is-not-defined)