How to do Integer Division in JavaScript

How to do Integer Division in JavaScript? In this article, we will go over how to use JavaScript’s bitwise operators and the Math package to find the quotient and remainder of a division. We will see how to do Integer division in JavaScript in the following two ways:-

  • Using Math Library
  • Using Bitwise Operators

JavaScript Integer Division’s Quotient and Remainder Using Math Library

JavaScript makes it simple to divide two variables, and the result is in floating-point numbers. However, the Math library, which offers a variety of functions, can be used to obtain the division’s quotient and remainder. For instance, the Math.floor() or Math.trunc() function, which transforms the floating-point value to an integer, can be used to obtain the quotient of a division, and the % character can be used to obtain the remainder.

Find the remainder and quotient of 11 divided by 5, for instance.

var a = 11;
var b = 5;
var quotient = Math.floor(a / b);
var remainder = a % b;
console.log('Quotient = ', quotient);
console.log('Remainder = ', remainder);

Output:-

Quotient = 2
Remainder = 1

Keep in mind that if the integers are too large, the Math library will crash. As an alternative to the Math.floor() function, you can alternatively use the Math.trunc() function, which can manage larger integers. Negative values will result in a failure of the Math.floor() function but not a failure of Math.trunc().

JavaScript Integer Division’s Quotient and Remainder Using Bitwise Operators

The bitwise operators in JavaScript allow us to obtain the quotient and remainder of a division. For instance, utilizing bitwise NOT or bitwise OR |0, which converts the floating-point number to an integer, we can obtain the quotient of a division. We may also use the% character to get the remaining value.

Find the remainder and quotient of 11 divided by 5, for instance. Check out the code below.

var a = 25;
var b = 9;
var quotient = ~~(a / b);
var remainder = a % b;
console.log('Quotient = ', quotient);
console.log('Remainder = ', remainder);

Output:-

Quotient = 2
Remainder = 7

So, the result is the same as with the procedure described above. When compared to the Math library, bitwise operators perform better, although they are less capable of handling huge numbers. Therefore, you can utilize the bitwise operators if your numbers are tiny; otherwise, use the Math library. Negative values can also be handled using the bitwise operators. The parseInt() function can also be used to change a floating-point number to an integer.

If you do not wish to utilize additional functions, you can employ the remaining operator % in a straightforward calculation like the one below.

var a = 25;
var b = 4;
var remainder = a % b;
var quotient = (a - remainder) / b;
console.log('Quotient = ', quotient);
console.log('Remainder = ', remainder);

Output:-

Quotient = 6
Remainder = 1

You’ll notice that the results are the same as with the procedures mentioned before.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.