HOWTO · JavaScript
Integer Division in JavaScript
Choose truncation or floor semantics to calculate an integer quotient and remainder correctly in JavaScript.
On this page
JavaScript has no // integer-division operator. For ordinary Number values, use Math.trunc(dividend / divisor) when the quotient should discard its fractional part toward zero, and use % for the remainder. Choose Math.floor() instead only when rounding down is the intended rule. These choices differ for negative values, so deciding the rule first is more important than choosing a shortcut.
Direct Answer: Choose Truncation or Floor Semantics
Integer division can mean two different things. A truncating quotient removes the fractional digits: Math.trunc(-13 / 5) is -2. A floor quotient rounds toward negative infinity: Math.floor(-13 / 5) is -3. For positive inputs both produce 2, which can hide the difference until a negative input reaches production.
Use truncation for tasks that split a signed quantity into a quotient and signed remainder. Use floor division for tasks such as zero-based buckets or cyclic indexing where the quotient must always round down. JavaScript’s % operator returns a remainder, not an always-non-negative mathematical modulo; its sign follows the dividend. MDN distinguishes the remainder operator from mathematical modulo.
Recommended Number Quotient and Remainder
For finite Number inputs, Math.trunc and % make the quotient/remainder relationship explicit. This is the recommended approach for a normal integer division task. Check that the divisor is not zero before calculating: a nonzero Number divided by signed zero produces signed Infinity, while 0 / 0 is NaN and a Number remainder with a zero divisor is also NaN. The ECMAScript specification defines these division and remainder cases.
const dividend = 13;
const divisor = 5;
console.log(`quotient: ${Math.trunc(dividend / divisor)}`);
console.log(`remainder: ${dividend % divisor}`);
quotient: 2
remainder: 3
The remainder can also help verify the result: for integer inputs, dividend equals divisor * quotient + remainder. This relationship is useful for validation, but computing the quotient with Math.trunc communicates the desired rounding rule more clearly than deriving it indirectly.
Negative Operands and Signed Remainder
Negative operands are the boundary that separates truncation from floor semantics. With -13 / 5, truncation gives -2; floor division gives -3. The % result is -3, because JavaScript keeps the dividend’s sign for a nonzero remainder. Neither operation fails for negative inputs; they simply answer different questions.
const dividend = -13;
const divisor = 5;
console.log(`truncating quotient: ${Math.trunc(dividend / divisor)}`);
console.log(`floor quotient: ${Math.floor(dividend / divisor)}`);
console.log(`remainder: ${dividend % divisor}`);
truncating quotient: -2
floor quotient: -3
remainder: -3
When Floor Division or Modulo Is Required
If the application requires a non-negative modulo for a positive divisor, normalize the remainder instead of assuming % already has that behavior. Pair it with Math.floor so both values use the same floor-division convention. The following example is appropriate for a five-item cycle, not for every quotient/remainder task.
const dividend = -13;
const divisor = 5;
const quotient = Math.floor(dividend / divisor);
const modulo = ((dividend % divisor) + divisor) % divisor;
console.log(`floor quotient: ${quotient}`);
console.log(`non-negative modulo: ${modulo}`);
floor quotient: -3
non-negative modulo: 2
This normalization assumes a positive divisor. Define and test the desired convention separately when a divisor may be negative.
Large Exact Integers With BigInt
Number uses IEEE-754 floating-point values. It cannot represent every integer exactly beyond the safe-integer range, so validate exact integer inputs with Number.isSafeInteger before relying on a Number quotient or remainder. For larger exact integers, use BigInt values consistently. BigInt division truncates toward zero, and its remainder follows the same signed-remainder idea.
const dividend = 13n;
const divisor = 5n;
console.log(`quotient: ${dividend / divisor}`);
console.log(`remainder: ${dividend % divisor}`);
try {
13n / 0n;
} catch (error) {
console.log(`zero divisor: ${error.name}`);
}
try {
13n / 5;
} catch (error) {
console.log(`mixed types: ${error.name}`);
}
quotient: 2
remainder: 3
zero divisor: RangeError
mixed types: TypeError
Do not mix Number and BigInt operands: convert deliberately before arithmetic. A BigInt zero divisor throws RangeError, unlike a Number zero divisor, so input validation remains useful in both cases. MDN documents BigInt division, mixed-type errors, and zero-divisor behavior.
Why Not Bitwise Coercion or parseInt
You may see ~~value or value | 0 used as a compact truncation idiom. Avoid those forms for general integer division: bitwise operators coerce to a signed 32-bit integer, so they change values outside that range. parseInt is for parsing strings; it is not a numeric rounding API. MDN documents Math.trunc and its distinction from bitwise conversion. The contrast below shows the 32-bit coercion boundary and the legitimate string-parsing behavior.
const value = 2 ** 32 + 1;
console.log(`Math.trunc: ${Math.trunc(value)}`);
console.log(`~~value: ${~~value}`);
console.log(`value | 0: ${value | 0}`);
console.log(`parseInt("13.8", 10): ${parseInt("13.8", 10)}`);
Math.trunc: 4294967297
~~value: 1
value | 0: 1
parseInt("13.8", 10): 13
Use Math.trunc for a numeric Number value and parseInt(text, 10) only when the input is text that should be parsed. Neither shortcut fixes floating-point precision; use BigInt when exact large integer arithmetic is required.
Decision Summary and Limitations
Use Math.trunc(a / b) and a % b for the usual JavaScript integer division task. Use Math.floor(a / b) with normalized modulo only when floor semantics are required. Use BigInt for exact large integers, reject a zero divisor, and never mix BigInt with Number in one operation. Avoid bitwise coercion and parseInt as general-purpose alternatives because they solve different problems.