42

What does the % do in JavaScript?

A definition of what it is and what it does would be much appreciated.

dakab
  • 4,576
  • 8
  • 38
  • 56
fancy
  • 41,315
  • 56
  • 147
  • 225

9 Answers9

35

It's a modulo operator. See this documentation or the specification for more information on JavaScript arithmetic operators.

% (Modulus)

The modulus operator is used as follows:

var1 % var2

The modulus operator returns the first operand modulo the second operand, that is, var1 modulo var2, in the preceding statement, where var1 and var2 are variables. The modulo function is the integer remainder of dividing var1 by var2. For example, 12 % 5 returns 2. The result will have the same sign as var1; that is, −1 % 2 returns −1.

T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
nulltoken
  • 55,645
  • 19
  • 127
  • 125
  • 1
    If you expect operator to return an integer (Ex. for calendar calculations), One should truncate var1 and var2 to integers before applying the %-operator. Modolus is defined like that, and one could not use % as is. Ex: Get last Sunday in march 2017. `31 - (((((5 * 2017) / 4) + 4) % 7)) == 25.75 // Not a valid date 31 - (~~((((5 * 2017) / 4) + 4) % 7)) == 26 // Correct` – Jesper Jensen May 12 '17 at 08:25
  • This answer is not quite correct. It's a remainder operator. They are not the same! https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Remainder – Jeroen Apr 02 '21 at 12:50
24

ES6 Update:

As explained in other answers, it returns the remainder after dividing the dividend by divisor, however this is no longer modulo operator, this is remainder operator. the difference being that the modulo operator result would take the sign of the divisor, not the dividend.Quoted from 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, not the divisor. It uses a built-in modulo function to produce the result, which is the integer remainder of dividing var1 by var2 — for example — var1 modulo var2. There is a proposal to get an actual modulo operator in a future version of ECMAScript, the difference being that the modulo operator result would take the sign of the divisor, not the dividend.

Example:

-10 % 3 // -1
10 % -3 // 1
Tushar
  • 78,625
  • 15
  • 134
  • 154
  • 1
    this answer illustrates the major difference from a 'true' modulo operator and the current JavaScript remainder operator when used with dividend and divisors with DIFFERENT signs. This is a nuance that has the potential to cause bugs in your algorithms if not accounted for. – Peter Nov 20 '19 at 17:48
11

It returns the remainder of a division operation. 5%2 returns 1

OpenCoderX
  • 5,613
  • 7
  • 29
  • 58
8

It is a modulo operator.
It calculates the remainder.

If we do 23 % 10,
first, we divide 23 by 10 which equals 2.3
then take .3 * (the divisor) 10
= 3

AbcAeffchen
  • 12,535
  • 15
  • 46
  • 62
MHShoman
  • 81
  • 1
  • 1
7

That would be the modulo operator.

It returns the remainder of a division operation:

var remainder = 3 % 2; // equals 1
Justin Niessner
  • 229,755
  • 35
  • 391
  • 521
5

Just in case, if someone is looking for a real modulo function (which would always get the sign of the divisor), you can use this:

function Modulo(num, denom)
{
    if (num%denom >= 0)
    {
        return Math.abs(num%denom);
    }
    else
    {
        return num%denom + denom;
    }
}

The Math.abs is to prevent the case -12%12 → -0, which is considered equal to 0 but displayed as -0.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Romain Bel
  • 81
  • 1
  • 3
3

Modulus (%) operator returns the remainder.

If either value is a string, an attempt is made to convert the string to a number.

alert(5%3) will alert 2

Baz1nga
  • 15,010
  • 3
  • 31
  • 59
2

In JavaScript, % is a remainder operator (Not a modulo operator).

remainder operator % (uses Math.truc()):

remainder = -5 % 3 = -2

How is it calculated ?

quotient  = Math.trunc(dividend / divisor) = Math.trunc(-5 / 3) = -1;
remainder = dividend - divisor * quotient  = -5 - (3 * -1) = -2

modulo function (uses Math.floor()):

modulo(-5,3) = 1

How is it calculated ?

quotient = Math.floor(dividend / divisor) = Math.floor(-5 / 3) = -2;
remainder = dividend - divisor * quotient = -5 - (3 * -2) = 1

For positive numbers, both remainder operator and modulo gives the same result.

5 % 3 = 2
modulo(5,3) = 2

In JavaScript, there is no build-in function for doing modulo operation, so you need to write on your own by using Math.floor() as above. Alternatively even you could easily write using the remainder operator as shown below.

function modulo(n, m) {
   return ((n % m) + m) % m;
}
SridharKritha
  • 5,151
  • 2
  • 34
  • 32
1

% performs as the modulo operator, return division reminder. Example:

<script>
    var x = 5;
    var y = 2;
    var z = x % y;
    alert(z);
</script>

This will alert 1.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Joy Das
  • 85
  • 2
  • 12