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

Answer from Mark Elliot on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Remainder
Remainder (%) - JavaScript | MDN
The remainder (%) operator returns the remainder left over when one operand is divided by a second operand. It always takes the sign of the dividend.
🌐
W3Schools
w3schools.com › jsref › jsref_oper_remainder.asp
JavaScript Remainder Operator
The remainder (%) operator returns the remainder after the first operand is divided by the second operand. It always takes the sign of the first operand. ... If you want to use W3Schools services as an educational institution, team or enterprise, ...
Discussions

math - How to perform an integer division, and separately get the remainder, in JavaScript - Stack Overflow
In JavaScript, how do I get: The whole number of times a given integer goes into another? The remainder? More on stackoverflow.com
🌐 stackoverflow.com
Understanding the JavaScript Modulo Operator
A quick peek at the documentation would reveal that % in JavaScript is not modulo, it's the remainder operator (or can be found in the ECMAScript specification ). This is confusing to many people, and unfortunately articles like these don't help by using the wrong terminology. The difference occurs when one of the values is negative. -21 % 4 The result of this is 3 for "mod" and -1 for "rem". This can be confusing behavior, if for example, to implement an isOdd function, you might think of something like this: function isOdd(n) { return n % 2 === 1 } However, this is wrong! It returns false for an input of -1. More on reddit.com
🌐 r/programming
9
0
December 12, 2023
Javascript - Setting the remainder operator

I think that's a typo, despite technically being "correct" (2 % 11 = 2).

More on reddit.com
🌐 r/learnprogramming
4
2
March 22, 2016
Finding a Remainder in JavaScript help me pease
Tell us what’s happening: Your ... https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/finding-a-remainder-in-javascript... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
December 21, 2018
🌐
freeCodeCamp
forum.freecodecamp.org › guide
freeCodeCamp Challenge Guide: Finding a Remainder in JavaScript - Guide - The freeCodeCamp Forum
October 16, 2019 - Finding a Remainder in JavaScript Hints Hint 1 The remainder operator % gives the remainder of the division of two numbers. var remainder = 11 % 3; //remainder gets the value 2
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-remainder-operator
JavaScript Remainder(%) Operator - GeeksforGeeks
July 23, 2025 - The remainder operator in JavaScript is used to get the remaining value when an operand is divided by another operand. In some languages, % is considered modulo.
🌐
Medium
medium.com › @stitans28 › javascript-remainder-operator-101-794f4df28c87
JavaScript Remainder Operator 101 | by Shawn Townsend | Medium
August 4, 2022 - JavaScript uses the % symbol to represent the remainder operator (also known as the modulo/modulus operator). The remainder operator works by dividing a value by another and when it isn’t evenly distributed the modulus operator returns the ...
Find elsewhere
🌐
Reddit
reddit.com › r/programming › understanding the javascript modulo operator
r/programming on Reddit: Understanding the JavaScript Modulo Operator
December 12, 2023 - The remainder is what the modulo operator calculates. A practical use case provided is in creating circular arrays, like cycling through a list of colors indefinitely with time. This is achieved by using the modulo operator to cycle through ...
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript tutorial › javascript remainder operator
JavaScript Remainder Operator
November 15, 2024 - In this tutorial, you'll learn about the JavaScript remainder operator (%) to get the remainder of a number divided by another number.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Remainder_assignment
Remainder assignment (%=) - JavaScript | MDN
July 8, 2025 - The remainder assignment (%=) operator performs remainder on the two operands and assigns the result to the left operand.
🌐
30 Seconds of Code
30secondsofcode.org › home › javascript › math › quotient and remainder of division
Calculate the quotient and remainder of a division in JavaScript - 30 seconds of code
December 28, 2023 - For example, divmod(8, 3) returns ... in JavaScript, we can use the built-in Math.floor() function to get the quotient and the modulo operator (%) to get the remainder of the division x / y....
🌐
Wikipedia
en.wikipedia.org › wiki › Modulo
Modulo - Wikipedia
2 weeks ago - Some calculators have a mod() function button, and many programming languages have a similar function, expressed as mod(a, n), for example. Some also support expressions that use "%", "mod", or "Mod" as a modulo or remainder operator, such as a % n or a mod n.
🌐
W3Schools
w3schools.com › js › js_arithmetic.asp
JavaScript Arithmetic
The modulus operator (%) returns the division remainder.
🌐
Josh W. Comeau
joshwcomeau.com › javascript › modulo-operator
Understanding the JavaScript Modulo Operator • Josh W. Comeau
To understand why this works, it's worth remembering our new model for division: we're trying to divide timeElapsed into 3 equally-sized groups, without any fractional or decimal values. The remainder will always be either 0, 1, or 2.
🌐
Stack Abuse
stackabuse.com › bytes › integer-division-and-finding-the-remainder-in-javascript
Integer Division and Finding the Remainder in JavaScript
September 6, 2023 - In this code, num1 % num2 performs the modulo operation and the result is stored in the remainder variable. When we log remainder to the console, we get 1, which is the remainder when 10 is divided by 3. JavaScript does not have a built-in operator for integer division.
🌐
Reddit
reddit.com › r/learnprogramming › javascript - setting the remainder operator
r/learnprogramming on Reddit: Javascript - Setting the remainder operator
March 22, 2016 -

Hi, Can anyone help me with the following. It's a question from https://www.freecodecamp.org/challenges/finding-a-remainder-in-javascript

Finding a Remainder in JavaScript

The remainder operator % gives the remainder of the division of two numbers.

Example

5 % 2 = 1 because
Math.floor(5 / 2) = 2 (Quotient)
2 * 2 = 4
5 - 4 = 1 (Remainder)

Usage

In mathematics, a number can be checked even or odd by checking the remainder of the division of the number by 2.

17 % 2 = 1 (17 is Odd)
48 % 2 = 0 (48 is Even)

Note

The remainder operator is sometimes incorrectly referred to as the "modulus" operator. It is very similar to modulus, but does not work properly with negative numbers.

Instructions

Set remainder equal to the remainder of 11 divided by 3 using the remainder (%) operator.

  • The variable remainder should be initialized

    Answer: var remainder;

  • The value of remainder should be 2

    Answer: var remainder = 2;

  • You should use the % operator

    Answer: var remainder = 2 % 11;

My question

I understand everything up until the final question in the instructions. That is, I don't understand how the answer 2 % 11 sets remainder equal to the remainder of 11 divided by 3 using the remainder (%) operator. Can anyone help me with this? Thanks.

🌐
Programmingsoup
programmingsoup.com › javascript-remainder-operator
JavaScript: Remainder (%) Operator
January 5, 2021 - The remainder (%) operator returns the remainder of a division. To refresh our memory, a division looks like this, Dividend / Divisor = Quotient.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Finding a Remainder in JavaScript help me pease - JavaScript - The freeCodeCamp Forum
December 21, 2018 - Tell us what’s happening: Your code so far // Only change code below this line var remainder; var %; remainder = 2; % = remainder; Your browser information: User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64; …
Top answer
1 of 3
10

If you think about the mathematical process to calculate modulus you might be able see how you can do this without having to resort to a bunch of case statements. Instead think of it this way, you're just calculating a remainder:

Given 2 numbers a and b, you can compute mod(a,b) by doing the following:

q = a / b;  //finding quotient (integer part only)
p = q * b;  //finding product
remainder = a - p;  //finding modulus

Using this idea, you should be able to transfer it to JS. You said you're not looking for the straight up answer so that's all I'll say!

Edit: here is the code, like I said in the comments it's exactly the pseudocode I posted above:

function modulo(a,b){
  q = parseInt(a / b);  //finding quotient (integer part only)
  p = q * b;  //finding product
  return a - p;  //finding modulus
}

This will return the exact same values as using %

2 of 3
0

You might be overthinking this. You basically stated the solution in your question:

I need to carry over whichever sign is on num1, and also return a positive number if the num2 is negative

The second part isn't accurate, but I suspect you just misspoke. A positive number should be returned when num2 is negative unless num1 is negative.

At any rate, the important takeaway is that if num1 is negative the result will be negative, and otherwise the result will be positive. The sign of num2 is discarded.

Starting the code you've written (which others will be quick to point out isn't the simplest solution), the fix is to compute the remainder using both numbers' absolute values, and then apply num1's original sign to the result.

function modulo(num1, num2) {
  var sign = num1 < 0 ? -1 : 1;
  var dividend = Math.abs(num1);
  var divisor = Math.abs(num2);

  if (dividend === 0) {
    return 0;
  }
  if (dividend === 0 || isNaN(dividend) || isNaN(divisor)) {
    return NaN;
  }
  if (dividend < divisor) {
    return sign * dividend;
  }
  
  var counter = dividend;
  while (counter >= divisor) {
    counter = counter - divisor;
  }
  return sign * counter;
}

console.log( 25 %  4, modulo( 25,  4));
console.log(-25 %  4, modulo(-25,  4));
console.log( 25 % -4, modulo( 25, -4));
console.log(-25 % -4, modulo(-25, -4));
.as-console-wrapper{min-height:100%;}