How can I perform JavaScript integer division and get the remainder separately?

In JavaScript, what’s the best way to:

  1. Get the whole number of times one integer divides into another (integer division)?
  2. Get the remainder from that division?

I’m looking for a clean and reliable approach to handle both parts of a JavaScript integer division—the quotient and the remainder—without using floating-point results.

Hey! I’ve worked with JavaScript integer division quite a bit, and in my experience, using Math.floor() is the easiest and most reliable way to get the quotient without running into floating-point results. You can pair that with the modulus operator % to grab the remainder separately. It’s simple, effective, and works consistently across any integer inputs.

Here’s an example:

function divide(a, b) {
    const quotient = Math.floor(a / b); // Integer division
    const remainder = a % b; // Remainder
    return { quotient, remainder };
}

const result = divide(11, 3);
console.log(result.quotient);  // 3
console.log(result.remainder); // 2

Math.floor(a / b) ensures you get the integer division result, while a % b will give you the remainder. This is one of the most straightforward and reliable approaches for handling JavaScript integer division.

Great approach! If you’re dealing with positive integers, I’ve found that using bitwise operations can be a neat, compact way to achieve JavaScript integer division. It works similarly to Math.floor() but can be a little faster in some cases. What you do is use the bitwise OR operator | 0, which truncates the decimal part of the division.

Here’s a quick example:

function divide(a, b) {
    const quotient = a / b | 0; // Bitwise OR to truncate the decimal
    const remainder = a % b;
    return { quotient, remainder };
}

const result = divide(11, 3);
console.log(result.quotient);  // 3
console.log(result.remainder); // 2

The bitwise OR | 0 truncates the result of a / b to an integer. This is a super fast method for JavaScript integer division, especially when you’re working with positive numbers. Just be mindful that this trick works best with positive integers—if your numbers can be negative, you might need to handle that differently.

Nice tips! If you’re into more of a mathematical approach or just like keeping things low-level, you could also manually compute the quotient by repeatedly subtracting b from a. It’s more work than the built-in methods, but it’s a fun way to see how integer division works under the hood.

Here’s how you could do it:

function divide(a, b) {
    let quotient = 0;
    while (a >= b) {
        a -= b;
        quotient++;
    }
    const remainder = a;
    return { quotient, remainder };
}

const result = divide(11, 3);
console.log(result.quotient);  // 3
console.log(result.remainder); // 2

In this version, the function keeps subtracting b from a until a is smaller than b, and it increments the quotient every time. The remainder is whatever’s left in a. It’s not as optimized as using Math.floor() or bitwise tricks, but it gives you a clear understanding of how JavaScript integer division works at its core without relying on built-in functions.