How to get negative result using modulo operator in JavaScript ?

26 Aug, 2021
The %(modulo) operator in JavaScript gives the remainder obtained by dividing two numbers. There is a difference between %(modulo) and remainder operator. When remainder or %(modulo) is calculated on positive numbers then both behave similar but when negative numbers are used then both behave differently.The JavaScript %(modulo) behaves like 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 method by default.Therefore, We will use Number.prototype to create a mod function which will return modulo of two numbers.Syntax:Number.prototype.mod = function(a) {
    // Calculate
    return this % a;
}
Below Programs illustrate the %(modulo) operator in JavaScript:Example 1: This example uses modulo operator (%) to perform operation.<script>  // JavaScript code to perform modulo (%)// operation on positive numbers      // mod() functionNumber.prototype.mod = function(a) {          // Calculate    return this % a;}      // Driver codevar x = 21;var b = 4;  // Call mod() functionvar result = x.mod(b);      // Print resultdocument.write("The outcome is: " + result);</script>                                            Output:The outcome is: 1
Example 2:<script>  // JavaScript code to perform modulo (%)// operation on negative numbers      // Use mod() functionNumber.prototype.mod = function(a) {          // Calculate    return this % a;}      // Driver codevar x = -21;var b = 4;  // Call mod()var result = x.mod(b);      // Print resultdocument.write("The outcome is: " + result);</script>                                Output:The outcome is: -1
Therefore, it is clear that why JavaScript %(modulo) gives negative result.
Syntax:
Number.prototype.mod = function(a) {
    // Calculate
    return this % a;
}

Below Programs illustrate the %(modulo) operator in JavaScript:
Example 1: This example uses modulo operator (%) to perform operation.
<script>  // JavaScript code to perform modulo (%)// operation on positive numbers      // mod() functionNumber.prototype.mod = function(a) {          // Calculate    return this % a;}      // Driver codevar x = 21;var b = 4;  // Call mod() functionvar result = x.mod(b);      // Print resultdocument.write("The outcome is: " + result);</script>                                            
Output:
The outcome is: 1

Example 2:
<script>  // JavaScript code to perform modulo (%)// operation on negative numbers      // Use mod() functionNumber.prototype.mod = function(a) {          // Calculate    return this % a;}      // Driver codevar x = -21;var b = 4;  // Call mod()var result = x.mod(b);      // Print resultdocument.write("The outcome is: " + result);</script>                                
Output:
The outcome is: -1

Therefore, it is clear that why JavaScript %(modulo) gives 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 % bSyntax: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:<script>  // JavaScript implementation of above approach      // Use mod() functionNumber.prototype.mod = function(b) {          // Calculate    return ((this % b) + b) % b;}      // Driver codevar x = -21;var b = 4;  // Call mod() functionvar result = x.mod(b);      // Print resultdocument.write("The outcome is: " + result + "</br>");      x = -33;b = 5;  // Call mod() functionresult = x.mod(b);      // Print resultdocument.write("The outcome is: " + result);</script>                        Output:The outcome is: 3
The outcome is: 2

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:
<script>  // JavaScript implementation of above approach      // Use mod() functionNumber.prototype.mod = function(b) {          // Calculate    return ((this % b) + b) % b;}      // Driver codevar x = -21;var b = 4;  // Call mod() functionvar result = x.mod(b);      // Print resultdocument.write("The outcome is: " + result + "</br>");      x = -33;b = 5;  // Call mod() functionresult = x.mod(b);      // Print resultdocument.write("The outcome is: " + result);</script>                        
Output:
The outcome is: 3
The outcome is: 2

ashkanheidaryfazel
javascript-operators
Picked
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
Hide or show elements in HTML using display property
Roadmap to Learn JavaScript For Beginners
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?