Modulus
The modulus (remainder) operator is %.
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script type="text/javascript">
var result = 26 % 5; //equal to 1
document.writeln(result); //1
</script>
</head>
<body>
</body>
</html>
If either of the operands isn't a number, it is converted to a number using the Number() casting function. An empty string is treated as 0, and the Boolean value of true is treated as 1.
The modulus operator has the following behaviors:
dividend | divided by | RESULT |
---|---|---|
number | number | regular arithmetic division |
infinite number | finite number | NaN |
finite number | 0 | NaN |
Infinity | Infinity | NaN |
finite number | infinite number | return dividend |
0 | nonzero | zero |
Home
JavaScript Book
Language Basics
JavaScript Book
Language Basics
Operators:
- JavaScript Operators
- Increment/Decrement Operators
- Increment/Decrement Operators for String, Boolean, Floating-point and Object
- Unary Plus and Minus
- Bitwise Not operator
- Bitwise AND
- Bitwise OR
- Bitwise XOR
- Left Shift
- Signed Right Shift
- Unsigned Right Shift
- Logical NOT
- Logical AND
- Logical OR
- Multiply
- Divide
- Modulus
- Add
- Subtract
- Relational Operators
- Equal and Not Equal
- Identically Equal and Not Identically Equal
- Conditional Operator
- Assignment Operators
- Comma Operator