In JavaScript, the quotient and remainder of an integer division can be obtained using the division (/) and remainder (%) operators along with different techniques for getting the integer quotient.
- The / operator performs division and may return a decimal value.
- The % operator returns the remainder after division.
- Math.floor() can be used to obtain the integer quotient.
- The double bitwise NOT (~~) operator can truncate the decimal part of the quotient.
- The right shift (>>) operator can also truncate the decimal part, but it converts the value to a signed 32-bit integer.
The following approaches can be used to find the quotient and remainder of an integer division in JavaScript.
Approach 1: Using Math.floor()
The Math.floor() method returns the largest integer less than or equal to a given number. It can be used with the division operator to obtain the quotient, while the % operator gives the remainder.
Working:
- Divide the dividend by the divisor using /.
- Use Math.floor() to obtain the integer quotient.
- Use % to calculate the remainder.
Syntax:
const quotient = Math.floor(dividend / divisor);
const remainder = dividend % divisor;
const dividend = 39;
const divisor = 5;
const quotient = Math.floor(dividend / divisor);
const remainder = dividend % divisor;
console.log("Quotient = " + quotient);
console.log("Remainder = " + remainder);
Output
Quotient = 7 Remainder = 4
Approach 2: Using the Double Bitwise NOT (~~) Operator
The double bitwise NOT (~~) operator can be used to remove the fractional part of a number. Applying it to the result of division gives the integer quotient.
Working:
- Divide the dividend by the divisor.
- Apply ~~ to truncate the fractional part.
- Use % to calculate the remainder.
Syntax:
const quotient = ~~(dividend / divisor);
const remainder = dividend % divisor;
const dividend = 39;
const divisor = 5;
const quotient = ~~(dividend / divisor);
const remainder = dividend % divisor;
console.log("Quotient = " + quotient);
console.log("Remainder = " + remainder);
Output
Quotient = 7 Remainder = 4
Note: Bitwise operators convert values to signed 32-bit integers. Therefore, ~~ is not suitable for integers outside the 32-bit signed integer range.
Approach 3: Using the Right Shift (>>) Operator
The right shift (>>) operator can be used to truncate the fractional part after division by shifting the resulting value by zero bits.
Working:
- Divide the dividend by the divisor.
- Apply >> 0 to convert the result to a signed 32-bit integer.
- Use % to calculate the remainder.
Syntax:
const quotient = (dividend / divisor) >> 0;
const remainder = dividend % divisor;
const dividend = 39;
const divisor = 5;
const quotient = (dividend / divisor) >> 0;
const remainder = dividend % divisor;
console.log("Quotient = " + quotient);
console.log("Remainder = " + remainder);
Output
Quotient = 7 Remainder = 4
Note: The >> 0 approach is limited to signed 32-bit integers. For general-purpose integer division, Math.floor() is clearer and safer. Also, for negative numbers, Math.floor() and bitwise truncation behave differently because Math.floor() rounds toward negative infinity, while bitwise conversion truncates toward zero.