Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to get negative result using modulo operator in JavaScript ?

  • Last Updated : 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:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

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() 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
    document.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() 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
    document.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 % 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:




    <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
    document.write("The outcome is: " + result + "</br>");
          
    x = -33;
    b = 5;
      
    // Call mod() function
    result = x.mod(b);
          
    // Print result
    document.write("The outcome is: " + result);
    </script>                        

    Output:

    The outcome is: 3
    The outcome is: 2
    



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!