JavaScript % (modulo) gives a negative result for negative numbers

Number.prototype.mod = function (n) {
  return ((this % n) + n) % n;
};

Taken from this article: The JavaScript Modulo Bug

Leave a Comment