# What does % do in JavaScript?

What does the % do in JavaScript?

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

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.

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
``````

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

That would be the modulo operator.

It returns the remainder of a division operation:

``````var remainder = 3 % 2; // equals 1
``````

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

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.

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`

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

``````<script>
var x = 5;
var y = 2;
var z = x % y;
</script>
``````

Please find the functional usage of the modulus(%) operator.you have an array of 9 names you need to list it in 3 each per line you can use modular operator.

`````` listNames=["krishna","vishnu","john","ali","guru","guruvan","tryer","ben"]
var text = "<div class='carousel-inner'>";
text += '<div class="item active">';
for (var i in listNames) {
text += '<div class="div_block" >' + listNames[i] + '</div>';
if (i % 3 == 2) {
text += '</div><div class="item">';
}
}
text += "</div>";
\$('#name-list').html(text);
``````