➕remainder (%)
remainder `r = a % b`, `r` has the same sign as `a`.
JS ⟩ operator ⟩ arithmetic ⟩ remainder (%)
a%b returns
a, ifa= ±0 /b= ±Infinity.r=a-b*qwhere:qis the integer such that:rhas the same sign asa, while being as close to 0 as possible.
// ⭐️ `a` decides the sign of the remainder
5 % 2 // 1
-5 % 2 // -1 ⭐ remainder (%) and modulo are different❗ 👉 remainder vs. modulo vs. mod
% operator
works for floating-point numbers too.
replit: modulo(a,b) vs. mod(a,b)
// ⭐️ remainder (a % b)
//
// ┌─── `a` decides the "sign" ⭐️
// │
5 % 3, // 2
5 % -3, // 2
-5 % 3, // -2
-5 % -3, // -2
// modulo(a, b)
// ┌─── `b` decides the "sign" ⭐️
// │
modulo( 5, 3), // 2
modulo( 5, -3), // -1
modulo(-5, 3), // 1
modulo(-5, -3), // -2
modulo(6.4, 2.3), // 1.8000000000000007
// mod(a, b) (in math)
// ┌─── always >= 0 ⭐️
// │
mod( 5, 3), // 2
mod( 5, -3), // 2
mod(-5, 3), // 1
mod(-5, -3), // 1Last updated
Was this helpful?