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

Taken from this article: The JavaScript Modulo Bug

Answer from Enrique on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-get-negative-result-using-modulo-operator-in-javascript
How to get negative result using modulo operator in JavaScript ? - GeeksforGeeks
July 12, 2025 - For Positive Numbers: Input: a ... operations are different. The JavaScript %(modulo) operator is nothing but the remainder operator, that's why it gives negative results on negative numbers....
🌐
Dustin John Pfister
dustinpfister.github.io › 2017 › 09 › 02 › js-whats-wrong-with-modulo
What is wrong with javaScript Modulo? | Dustin John Pfister at github pages
April 19, 2021 - So then the javaScript modulo will work okay when it comes to example such as this where positive numbers are always what will be used. However the operator might not get the values that one might want when it comes to something that might involve numbers that will go in a negative range.
🌐
Codecademy Forums
discuss.codecademy.com › frequently asked questions › javascript faq
How does Javascript handle a negative modulo? (`-5 % 20`) - JavaScript FAQ - Codecademy Forums
January 11, 2020 - -5 % 20 should be 15. When I enter this it returns -5. Is this a javascript thing or an artifact of this simulator? thanks.
🌐
Mimo
mimo.org › glossary › javascript › modulo-operator
JavaScript Modulo Operator: Syntax, Usage, and Examples
Learn how JavaScript's modulo operator works with positive and negative numbers, floating points, and loops. Includes syntax, tips, and real examples.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Remainder
Remainder (%) - JavaScript | MDN
In JavaScript, the modulo operation (which doesn't have a dedicated operator) is used to normalize the second operand of bitwise shift operators (<<, >>, etc.), making the offset always a positive value.
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › modulus
The Modulus Operator in JavaScript - Mastering JS
For example, -21 % 5 === -1, because the remainder always takes the sign of the left number. However, a true modulus operator would always return a positive value, so 21 modulo 5 would equal 4. In practice, you are unlikely to use the remainder operator on negative values, and many JavaScript developers aren't aware of the difference.
Find elsewhere
🌐
Codecademy Forums
discuss.codecademy.com › frequently asked questions › javascript faq
How does Javascript handle a negative modulo? (`-5 % 20`) - Page 2 - JavaScript FAQ - Codecademy Forums
August 25, 2021 - JavaScript uses a remainder operator, not a modulo operator - which is why it’s incorrect to refer to it as the latter. Programming languages use one or the other. This typically does not cause an issue, as the result when dividing positive numbers is the same for both.
🌐
Medium
thomaspoignant.medium.com › how-to-get-always-a-positive-modulo-remainder-9ac965361ff4
How to always get a positive modulo remainder | by Thomas Poignant | Medium
July 10, 2020 - Some languages like python return the modulus and some others like java or javascript are returning the remainder. When the % operator is returning the remainder, you can have a negative modulo like in this example (in Javascript) :
🌐
Reddit
reddit.com › r/programming › understanding the javascript modulo operator
r/programming on Reddit: Understanding the JavaScript Modulo Operator
December 12, 2023 - A quick peek at the documentation would reveal that % in JavaScript is not modulo, it's the remainder operator (or can be found in the ECMAScript specification). This is confusing to many people, and unfortunately articles like these don't help by using the wrong terminology. The difference occurs when one of the values is negative.
🌐
Codingem
codingem.com › home › javascript % operator: an ultimate guide to modulos
JavaScript % Operator: An Ultimate Guide to Modulos - codingem.com
July 10, 2025 - In JavaScript, you can use the % operator to calculate modulo. ... You can compute modulo with numeric types int and float and negative numbers.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › modulus-on-negative-numbers
Modulus on Negative Numbers - GeeksforGeeks
July 23, 2025 - JavaScript · var a = 7; var b = -5; document.write(a % b ); // This code is contributed by ksrikanth0498. Output · 2 · The sign of remainder is negative using floor division to determine remainder when only divisor is negative.
🌐
JavaScript in Plain English
javascript.plainenglish.io › going-around-in-circles-with-modulus-in-javascript-9e4534ac47b1
Going around in circles with modulus in JavaScript | by Eric Loucks | JavaScript in Plain English
October 4, 2019 - As with the integer and rational ... b land, we can say that c = -a. Since JavaScript is giving us the negation of the correct modulus, adding b gives us the number we want!...
🌐
GitHub
gist.github.com › stevenkuipers › 6813c1962e622b5f083bdcc7eb31d75d
JavaScript doesn't process modulo values correctly when you start with a negative number. This function does. · GitHub
JavaScript doesn't process modulo values correctly when you start with a negative number. This function does. - modulo_negative_numbers.js
🌐
EyeHunts
tutorial.eyehunts.com › home › javascript modulus negative
JavaScript modulus negative
April 24, 2023 - The modulus operator in JavaScript follows the sign of the dividend, which means that if the dividend is negative, the result will also be negative.
🌐
GitHub
github.com › josdejong › mathjs › issues › 1964
Modulo operation for negative values · Issue #1964 · josdejong/mathjs
September 11, 2020 - I noticed that A mod N (or mod(A, N)) operation in MathJS doesn't return expected values ("expected" in terms of modular arithmetic) for negative values of A. For example, -5 mod 3 returns -2 while the correct result should be 1 (example...
Author   ovk
🌐
AlgoCademy
algocademy.com › link
Modulo Operator in JavaScript | AlgoCademy
Negative Numbers: The behavior of the modulo operator with negative numbers can vary between programming languages. In JavaScript, the result of a % b will have the same sign as a.
🌐
Rip Tutorial
riptutorial.com › remainder / modulus (%)
JavaScript Tutorial => Remainder / Modulus (%)
The remainder / modulus operator ... when one operand is divided by a second operand. When the first operand is a negative value, the return value will always be negative, and vice versa for positive values....
🌐
2ality
2ality.com › 2019 › 08 › remainder-vs-modulo.html
Remainder operator vs. modulo operator (with JavaScript code)
The reason for that is that Math.trunc() and Math.floor() produce the same results for positive numbers, but different results for negative numbers.