The remainder (%) operator returns the remainder left after dividing one number by another. It is commonly used in arithmetic calculations and conditional logic.
- Returns the remainder after dividing one number by another.
- Commonly used to check even/odd numbers and cycle through values.
- Returns NaN for invalid operations, and the result has the same sign as the dividend (left operand).
Syntax:
a%bReturn Type: number (the remainder of the division, or NaN for invalid operations)
Example 1: Positive Numbers and Infinity
console.log(100%23);
console.log(Infinity%20);
Output
8 NaN
Example 2: Negative Numbers and NaN
console.log(-100%23);
console.log(NaN%20);
Output
-8 NaN