javascriptmathmodulointeger-division

How to perform an integer division, and separately get the remainder, in JavaScript


In JavaScript, how do I get:

  1. The whole number of times a given integer goes into another?
  2. The remainder?

Solution

  • For some number y and some divisor x compute the quotient (quotient)[1] and remainder (remainder) as:

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

    Example:

    const quotient = Math.floor(13/3); // => 4 => the times 3 fits into 13  
    const remainder = 13 % 3;          // => 1
    

    [1] The integer number resulting from the division of one number by another