whatstheoutput_

#28: -7 % 3 and 7 % -3

Topic: numbers · Difficulty: ★★

console.log(-7 % 3);
console.log(7 % -3);

Output

-1
1

Why

JavaScript's % is a remainder operator, not a mathematical modulo — the result always takes the SIGN OF THE DIVIDEND (the left operand), regardless of the divisor's sign. -7 % 3 divides -7 by 3, which goes toward zero to -2 with a remainder of -1, so the result is -1. Flip it around: 7 % -3 divides 7 by -3 toward zero to -2, leaving a remainder of 1. Languages with true modulo semantics (like Python) would instead always return a result matching the sign of the divisor.

More numbers puzzles

Play today's puzzle →