JavaScript Tutorial/Operators/Mod

Материал из Web эксперт
Перейти к: навигация, поиск

Mod operator in action

<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
<!--
var a = 6, b = 4;
alert(a % b);
//  -->
</script>
</head>
<body>
</body>
</html>


Modulus (%)

Modulus (%) operator returns only the remainder.

If either value is a string, an attempt is made to convert the string to a number.

For example, the following line of code



var resultOfMod = 26 % 3;


%= (Modulus Assignment)

The modulus assignment operator divides the value stored in the left variable by the right value. If value is a string, an attempt is made to convert the string to a number before performing the modulus and assignment.



<html>
     <script language="JavaScript">
    <!--
    answer = 3;
    answer %= 17;
    document.write("answer = ",answer);
    -->
    </script>
</html>