Saturday, October 25, 2025
HomeLanguagesJavascriptJavaScript Remainder(%) Operator

JavaScript Remainder(%) Operator

The remainder operator in JavaScript is used to get the remaining value when an operand is divided by another operand. In some languages, % is considered modulo. Modulo and Remainder work differently when the sign of both operands is different.

In JavaScript remainder takes the sign of the dividend and to get modulo ((a % n) + n) % n should be used instead of a % n.

Syntax

remainder = var1 % var2

Example 1: This example returns the positive remainder in this case both modulo and remainder will be the same as both operands are positive.

Javascript




<script>
    // Initializing variables
    var a =4
    var n = 2
     
    // Calculating remainder
    var rem = a%n
     
    // Calculating modulo
    var mod = ((a%n)+n)%n
     
    // Printing result
    console.log("Remainder is "+rem)
    console.log("Modulo is "+mod)
</script>


Output:

"Remainder is 0"
"Modulo is 0"

Example 2: This example returns a negative remainder as the dividend is negative.

Javascript




<script>
    // Initializing variables
    var a =-4
    var n = 2
     
    // Calculating remainder
    var rem = a%n
     
    // Calculating modulo
    var mod = ((a%n)+n)%n
     
    // Printing result
    console.log(rem)
    console.log(mod)
</script>


Output:

-0
0

Example 3: Remainder with Infinity and NaN

Javascript




<script>
    // Both operands are NaN
    console.log(NaN%NaN)
     
    // Dividend is NaN
    console.log(NaN%2)
     
    // Dividend is Nan and Divisor is Infinity
    console.log(NaN%Infinity)
     
    // Dividend is Infinity and Divisor is NaN
    console.log(Infinity%NaN)
     
    // Both operands are Infinity
    console.log(Infinity%Infinity)
     
    // Dividend is infinity
    console.log(Infinity%5)
</script>


Output:

NaN
NaN
NaN
NaN
NaN
NaN

We have a complete list of Javascript Operators, to check those please go through the Javascript Operators Complete Reference article.

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS