The %(modulo) operator in JavaScript gives the remainder obtained by dividing two numbers. There is a difference between the %(modulo) and the remainder operator. When remainder or %(modulo) is calculated on positive numbers then both behave similarly but when negative numbers are used then both behave differently.
The JavaScript %(modulo) behaves like a remainder operation and gives the remainder and as the number is negative therefore remainder also comes out to be negative.
Let’s understand and compare %(modulo) and remainder operation results clarity.
Examples of Modulo Operator:
For Positive Numbers: Input: a = 21, b = 4 Output: 1 Explanation: modulo = 21 % 4 modulo = 21 - 4 * 5 modulo = 21 - 20 = 1 Other Explanation: The number 21 can be written in terms of 4 as 21 = 5 * 4 + 1 So, here '1' is the result. For Negative Numbers: Input: a = -23, b = 4 Output: 1 Explanation: modulo = -23 % 4 modulo = -23 + 4 * 6 modulo = -23 + 24 = 1 Other Explanation: The number -23 can be written in terms of 4 as -23 = (-6) * 4 + 1 So, here '1' is the result.
Examples of Remainder operator:
Remainder operator uses the formula: Remainder = a - (a / b) * b Note: Result of (a / b) is first converted into Integer Value. For Positive Numbers: Input: a = 21, b = 4 Output: 1 Explanation: Remainder = 21 - (21 / 4) * 4 Remainder = 21 - 5 * 4 Remainder = 21 - 20 = 1 For Negative Numbers: Input: a = -23, b = 4 Output: -3 Explanation: Remainder = -23 -( -23 / 4) * 4 Remainder = -23 -(-5) * 4 Remainder = -23 + 20 = -3
So, from the above comparison, it is clear that both the remainder and modulo operations are different. The JavaScript %(modulo) operator is nothing but the remainder operator, that’s why it gives negative results on negative numbers.
Number.prototype: The prototype constructor allows adding new properties and methods to JavaScript numbers such that all numbers get this property and can access the method by default. Therefore, We will use the Number.prototype to create a mod function that will return the modulo of two numbers.
Syntax:
Number.prototype.mod = function(a) { // Calculate return this % a; }
The below Programs illustrate the %(modulo) operator in JavaScript:
Example 1: This example uses the modulo operator (%) to perform the operation.
javascript
<script> // JavaScript code to perform modulo (%) // operation on positive numbers // mod() function Number.prototype.mod = function (a) { // Calculate return this % a; } // Driver code var x = 21; var b = 4; // Call mod() function var result = x.mod(b); // Print result console.log( "The outcome is: " + result); </script> |
Output:
The outcome is: 1
Example 2:
javascript
<script> // JavaScript code to perform modulo (%) // operation on negative numbers // Use mod() function Number.prototype.mod = function (a) { // Calculate return this % a; } // Driver code var x = -21; var b = 4; // Call mod() var result = x.mod(b); // Print result console.log( "The outcome is: " + result); </script> |
Output:
The outcome is: -1
Therefore, it is clear why JavaScript %(modulo) gives a negative result.
Making changes in JavaScript %(modulo) to work as a mod operator: To perform modulo operator (%) like actual modulo rather than calculating the remainder. We will use the following formula. Suppose numbers are a and b then calculate mod = a % b
Syntax:
Number.prototype.mod = function(b) { // Calculate return ((this % b) + b) % b; }
In the above formula, we are calculating modulo from remainder using modular property (a + b) mod c = (a mod c + b mod c) mod c. Below Program illustrates the above approach.
Example: This example explains the above approach.
javascript
<script> // JavaScript implementation of above approach // Use mod() function Number.prototype.mod = function (b) { // Calculate return (( this % b) + b) % b; } // Driver code var x = -21; var b = 4; // Call mod() function var result = x.mod(b); // Print result console.log( "The outcome is: " + result); x = -33; b = 5; // Call mod() function result = x.mod(b); // Print result console.log( "The outcome is: " + result); </script> |
Output:
The outcome is: 3 The outcome is: 2