Modulo Operator %%

*copied from es-discuss mailing list

JS needs a modulo operator. It currently has the remainder operator % which works in most cases except for negative values. I believe %% would work great and be easy to remember.

let x = (-13) %% 64;

is equivalent to

let x = ((-13 % 64) + 64) % 64;


Is this a request for a proper modulus operator instead of just the builtin remainder operator?

Would a Math.mod function suffice? That would avoid the need for new syntax and could be polyfilled via, IIRC

Math.mod = (a, b) => ((a % b) + b) % b

See also:

1 Like

Yes, Math.mod would work, but I believe %% would be a nice addition similar to how we have Math.pow and **, the exponentiation operator.