JavaScript Integer Division [3 Methods]


JavaScript

Introduction

Mathematical operations are at the core of programming and computers, and it's because they can do them so much faster than the average human that they have become popular.

One such operation is the division which can be quite complex but made a lot easier with integer division. With typical divisions, there can be a bit of approximation which can be difficult to work with when precision calculations are required (and especially with JavaScript where certain functions are implementation-dependent).

However, with integer division, you can divide two integers and discard the remainder (if any), especially if you only need the number of times (quotient) a number is divisible by another number. In this article, we will discuss how to achieve integer division in JavaScript.

 

JavaScript Integer Division Methods

We will make use of two methods and one operator to achieve JavaScript integer division.

 

Use floor() method

Within JavaScript, there is an all-powerful Math object that provides a ton of mathematical functionalities from constants to functions. Important to note that, the Math object doesn’t work with BigInt. Within the Math object are static properties and methods that provide mathematics functionalities.

One such static method is the floor method that rounds down its Number type argument. So, for us to achieve integer division, we can do the typical division using the / operator and round down the answer (a floating-point number) using the floor method to give us the quotient - the integer division result.

const y = 34;
const x = 12;

const quotient = Math.floor(y / x);

console.log(quotient);

Output

2

If you need the remainder, you can make use of the modulus operator

const y = 34;
const x = 12;

const quotient = Math.floor(y / x);
const remainder = y % x;

console.log(`quotient - ${quotient}, remainder - ${remainder}`);

Output

quotient - 2, remainder - 10

It also works with negative numbers.

const y = -3231134;
const x = 12;

const quotient = Math.floor(y / x);
const remainder = y % x;

console.log(`quotient - ${quotient}, remainder - ${remainder}`);

Output

quotient - -269262, remainder - -2

 

Use trunc() method

While the floor method rounds down, the trunc method simply returns the integer part of a number and removes any fractional digits that are present within a Number value.

So, we can carry out the same operation by changing the floor method to the trunc method to achieve integer division in JavaScript.

const y = 34;
const x = 12;

const quotient = Math.floor(y / x);
const remainder = y % x;

console.log(`quotient - ${quotient}, remainder - ${remainder}`);

Output

quotient - 2, remainder - 10

 

Use the Bitwise Operator method

There are 7 bitwise operators that can perform binary-based actions on their operands, and treats their operands as a set of 32-bit binary digits. Important to us however is the bitwise NOT operator which inverts the bits of its operand and can help us convert the floating-point number to an integer.

const y = 34;
const x = 12;

const quotient = ~~(y / x);
const remainder = y % x;

console.log(`quotient - ${quotient}, remainder - ${remainder}`);

Output

quotient - 2, remainder - 10

So, the code inverts the bits of the division of the numbers twice to give the integer result that we need.

 

Summary

To achieve JavaScript integer division, we can make use of two static methods from the Math object - floor and trunc - and one bitwise operator - NOT. With the aid of these features, we can scale integer divisions from small to large numbers. However, all methods don’t work with BigInt numbers.

 

References

Math - JavaScript | MDN (mozilla.org)
Math.floor() (mozilla.org)
Math.trunc() - JavaScript | MDN (mozilla.org)

 

Olorunfemi Akinlua

Olorunfemi Akinlua

He is boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs user-friendly interfaces to enhance digital experiences across various domains. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment