The problem with % is that it is a remainder operator with truncated division, not a modulo one with floored division. When the divisor (i-1) becomes negative, so does the result. You can use

if (--i < 0) i = stuff.length - 1;

or

i = (i + stuff.length - 1) % stuff.length;

instead (which only work for input values of i in the expected range, though)

Answer from Bergi on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Remainder
Remainder (%) - JavaScript | MDN
For two values of the same sign, ... which can make them differ by one unit of d. To obtain a modulo in JavaScript, in place of n % d, use ((n % d) + d) % d....
🌐
Josh W. Comeau
joshwcomeau.com › javascript › modulo-operator
Understanding the JavaScript Modulo Operator • Josh W. Comeau
It's guaranteed to always cycle within the range of available indexes for that array. To understand why this works, it's worth remembering our new model for division: we're trying to divide timeElapsed into 3 equally-sized groups, without any fractional or decimal values.
🌐
Codedamn
codedamn.com › news › javascript
JavaScript Modulo Operator Guide With Examples
June 3, 2023 - Here are some common use cases ... as well. For example: ... A: The modulo operator (%) returns the remainder of a division operation, while the division operator (/) returns the quotient....
🌐
W3Schools
w3schools.com › js › js_arithmetic.asp
JavaScript Arithmetic
In arithmetic, the division of two integers produces a quotient and a remainder. In mathematics, the result of a modulo operation is the remainder of an arithmetic division.
🌐
LogRocket
blog.logrocket.com › home › mastering the modulo operator in javascript: a complete guide
Mastering the modulo operator in JavaScript: A complete guide - LogRocket Blog
June 4, 2024 - For example, when we divide 10 by three, we get a quotient of three and a remainder of one. We can also write this as 1 (mod 3). In JavaScript, the modulo operator gets the remainder of a division operation.
🌐
30 Seconds of Code
30secondsofcode.org › home › javascript › math › quotient and remainder of division
Calculate the quotient and remainder of a division in JavaScript - 30 seconds of code
December 28, 2023 - Its purpose is to return a 2-tuple consisting of the quotient and remainder of a division. For example, divmod(8, 3) returns (2, 2) because 8 / 3 = 2 with a remainder of 2. In order to implement divmod() in JavaScript, we can use the built-in ...
🌐
DEV Community
dev.to › timothee › using-modulo-to-shift-a-value-and-keep-it-inside-a-range-8fm
Using modulo to shift a value and keep it inside a range - DEV Community
February 2, 2024 - E.g., index in an array, hours in a day, degrees on a compass. First of all, a quick definition: the modulo operator gives the remainder of a division of one number by another. In JavaScript the modulo operator is %.
Find elsewhere
🌐
LabEx
labex.io › tutorials › javascript-quotient-and-module-of-division-28277
Quotient and Module of Division in JavaScript | LabEx
We will learn how to implement this function using the Math.floor() and modulo operators, and use it to perform various division operations. By the end of this lab, you will have a solid understanding of how to use this function to solve problems involving division in your JavaScript programs. To practice coding, open the Terminal/SSH and type node. This code returns an array that consists of the quotient and remainder of the given numbers.
🌐
John Kavanagh
johnkavanagh.co.uk › home › articles › using the modulo operator in javascript
Using the Modulo Operator in JavaScript, by John Kavanagh
February 27, 2024 - Here, I'm using modulo to calculate the following index in an array, rolling back to 0 if we've reached the end of the array. The modulo operator in JavaScript is a simple yet powerful tool that extends beyond basic arithmetic. It's extremely useful for scenarios like checking divisibility, looping ...
🌐
freeCodeCamp
freecodecamp.org › news › javascript-modulo-operator-how-to-use-the-modulus-in-js
JavaScript Modulo Operator – How to Use the Modulus in JS
November 7, 2024 - For example, 10 modulo 3 will be ... for JavaScript, you will use 10 % 3. It divides the first operand (the dividend) by the second operand (the divisor) and returns the remainder....
🌐
AlgoCademy
algocademy.com › link
Modulo Operator in JavaScript | AlgoCademy
The key concept behind the modulo operator is its ability to determine the remainder of a division operation. This can be particularly useful in various programming scenarios: Even or Odd: You can use the modulo operator to check if a number is even or odd. If number % 2 equals 0, the number is even; otherwise, it is odd. Cycling Through Indices: When working with arrays, the modulo operator can help cycle through indices, ensuring they stay within bounds.
🌐
Reddit
reddit.com › r/programming › understanding the javascript modulo operator
r/programming on Reddit: Understanding the JavaScript Modulo Operator
December 12, 2023 - The author clarifies its function by redefining how we think about division, emphasizing that it's about dividing a number into equally-sized groups without fractional remainders.
🌐
Medium
medium.com › @seanmcp › js-basics-remainder-modulo-bed750a000b
JS Basics: Remainder / Modulo. When you were first learning division… | by Sean McPherson | Medium
October 17, 2017 - If a number is divisible by two, then x % 2 will equal 0. With that in mind, we can write a function that iterates through an array and uses a modulo to check if each number is even.
🌐
Banjocode
banjocode.com › post › javascript › iterate-array-with-modulo
Iterate Over An Array With The Modulo Operator | banjocode
If we would use a normal for loop, the array would be out of bounds quite soon if we didn’t manually reset the index. We can use the modulo operator to fix this problem without having to use to much logic.
🌐
Rip Tutorial
riptutorial.com › remainder / modulus (%)
JavaScript Tutorial => Remainder / Modulus (%)
x % 4 == 0 // true if x is divisible by 4 x % 2 == 0 // true if x is even number x % 2 != 0 // true if x is odd number
🌐
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 the remainder in division. The % operator is called the modulo operator.
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › modulus
The Modulus Operator in JavaScript - Mastering JS
The modulus operator in JavaScript returns the remainder when the first operand is divided by the second operand. Here's what you need to know.
🌐
Mimo
mimo.org › glossary › javascript › modulo-operator
JavaScript Modulo Operator: Syntax, Usage, and Examples
Mastering the modulo operator will ... and array indexing. It also pairs well with concepts found in ecmascript, where numerical behavior stays consistent across environments. The syntax of the JavaScript modulo operator is similar to that of basic arithmetic operators: ... Since this operation produces a remainder, it's directly related to the concept of a quotient, which represents the whole-number result of the division ...