In JavaScript, the division of numbers can be done using the division operator /
. However, this will result in a floating-point number if the division isn’t exact. If you want to perform integer division, which discards the remainder and only returns the quotient, you have a couple of options.
Best Solution: Math.trunc()
The Math.trunc()
function removes the fractional part of a number, effectively rounding towards zero. It works similarly to Math.floor()
for positive numbers, but differently for negative numbers.
let dividend = -10;
let divisor = 3;
let quotient = Math.trunc(dividend / divisor);
console.log(quotient); // Output: -3
In this example, -10 / 3
is -3.333...
, and Math.trunc()
rounds it towards zero to -3
.
To get the remainder, you can use the Modulus operator (%
):
let dividend = -10;
let divisor = 3;
let quotient = Math.trunc(dividend / divisor); // -3
let remainder = dividend % divisor; // -1
Using the ParseInt() Function
The parseInt()
function in JavaScript can convert a string or number to an integer. By default, it discards any fractional part of a number.
let dividend = 10;
let divisor = 3;
let quotient = parseInt(dividend / divisor);
console.log(quotient); // Output: 3
In this example, parseInt(10 / 3)
converts 3.333...
to 3
Using Math.floor() and Math.ceil()
Before the introduction of Math.trunc()
in ES6, developers used Math.floor()
for positive numbers and Math.ceil()
for negative numbers to discard the decimal part of the division result.
let dividend = 10;
let divisor = 3;
let quotient = Math.floor(dividend / divisor);
console.log(quotient); // Output: 3
dividend = -10;
quotient = Math.ceil(dividend / divisor);
console.log(quotient); // Output: -3
Remember, if you don’t know beforehand if the numbers you’re working with are positive or negative, you have to handle both cases:
function divideInt(dividend, divisor) {
if (divisor === 0) {
throw new Error('Cannot divide by zero!');
}
let divisionResult = dividend / divisor;
if (divisionResult > 0) {
return Math.floor(dividend / divisor);
} else if (divisionResult < 0) {
return Math.ceil(dividend / divisor);
}
return divisionResult;
}
console.log(divideInt(10, 3)); // Output: 3
Bitwise OR Operator
The bitwise OR operator (|
) can also be used to perform integer division in JavaScript. It works by first converting its operands to 32-bit integers, effectively removing any decimal part.
let dividend = 10;
let divisor = 3;
let quotient = (dividend / divisor) | 0;
console.log(quotient); // Output: 3
In this example, (10 / 3) | 0
performs the division, converts the result to a 32-bit integer, and returns 3
.
Keep in mind that the bitwise OR operator method should be used with caution, as it can give unexpected results for large numbers or negative numbers due to the way JavaScript handles 32-bit integers.
Double Bitwise NOT Operator
The double bitwise NOT operator (~~
) is another way to truncate any fractional part of a number in JavaScript. Like the single bitwise OR operator, it first converts its operands to 32-bit integers.
let dividend = 10;
let divisor = 3;
let quotient = ~~(dividend / divisor);
console.log(quotient); // Output: 3
In this example, ~~(10 / 3)
performs the division, converts the result to a 32-bit integer, and returns 3
. Note that this method also has the same limitations on large and negative numbers as the bitwise OR operator.
Right Shift Bitwise Operator
The right shift operator (>>
) shifts all bits in the binary representation of a number to the right by a specified number of places. If you shift by zero places, it effectively performs integer division by rounding towards zero.
let dividend = 10;
let divisor = 3;
let quotient = (dividend / divisor) >> 0;
console.log(quotient); // Output: 3
In this example, (10 / 3) >> 0
performs the division, shifts the result zero places to the right (which removes the fraction), and returns 3
.
Note that like other bitwise operators, the right shift operator first converts its operands to 32-bit integers, so it has the same limitations with large numbers or negative numbers. Also, it rounds towards zero, not downward, when dealing with negative numbers.