const remainder = 15 % 7; document.write(remainder); // output : 1
Here, we have created content namely “remainder” and assigned the value 15 % 7. The whole thing will give us the remainder of 15 divided by 7. It will be equal to 2 remainders 1, so 1 will be stored in the remainder. Therefore, output 1 is displayed.
-15 % 7;
If we give the negative value of the first operator then the result will return a negative value. Here, we have the negative value for the first operator (-15) then put the modulus operator and give a negative or positive value i.e 7. So, the result is “-1”
15 % -7;
But if you set the first operator to positive and the second operator to negative, you will get a positive value. For example, see the above expression, we have the first operator in positive and the second operator in the negative, so the result is returned a positive value of 1.