What is an operator in programming — and why -1 % 5 breaks your carousel

You asked an AI for an image carousel with "next" and "back" arrows. Next works perfectly, round and round. But on the first image you hit "back" — and the carousel goes blank. No error, no image.
The culprit is one character: %. It's an operator. Small, easy to miss — and one of the most useful things in code. Let's figure out what operators are, then fix the carousel.
What an operator is — in one line
An operator is a short symbol that takes one or two values and produces a new one. 2 + 3 — the plus took a two and a three and produced a five. That's the whole definition.
The values on either side are called operands. The operator itself is like a machine on a workbench: you put parts in on both sides and get a finished piece out. There are many machines, each with its own job.
The operators you'll meet most
You'll see them in any AI-written code. They fall neatly into five families:
- Arithmetic:
+,-,*,/,%(remainder),**(power).2 ** 10is 1024. - Comparison:
===,!==,>,<,>=. They answer "yes" or "no" — they produce a boolean. - Logic:
&&("and"),||("or"),!("not"). They glue several conditions into one. - Assignment:
=,+=,++. They put a value into a variable:score += 10means "add ten to the score". - Fallback:
??and?.. They save you when a value might be missing. More in what is null.
The tricky part: the same symbol behaves differently depending on what's on either side. 2 + 3 is 5. But "2" + "3" is "23": for text, plus means "glue". More on that in what is a string.
The remainder — a quiet hero
% has nothing to do with percent. It returns the remainder of a division. 7 % 2 is 1: seven divided by two leaves one over. 10 % 3 is 1 too.
Sounds like school boredom. But look how many tasks it solves in one line:
- Even or odd.
n % 2 === 0means even. That's where "zebra" tables come from: every other row a different color. - Every third.
i % 3 === 0— insert an ad after every third card in a feed. - Minutes and seconds. You have 125 seconds.
Math.floor(125 / 60)is 2 minutes,125 % 60is 5 seconds. A "2:05" timer is done. - Going in circles. The carousel has five images. The next one is
(i + 1) % 5. After the fourth (counting from zero) comes5 % 5, which is 0 — the first one again.
That's why "next" worked in your carousel. The trouble was "back".
The trap: minus and remainder
You'd expect "back" from the first image to be (0 - 1) % 5, meaning the last one, number four. But JavaScript answers: -1 % 5 is -1. Not 4.
In JavaScript the remainder takes the sign of the number being divided. Negative dividend — negative remainder. There's no image number -1, so the carousel shows emptiness. No error — just undefined where an image was expected.
The fix is one line:
const prev = ((i - 1) % total + total) % total
// i = 0, total = 5 → ((−1) + 5) % 5 = 4 — the last image
Adding the list length lifts you out of the negatives. The second % brings the number back into range. It works for any step backwards.
Order of operations: like school, but not quite
2 + 3 * 4 is 14, not 20. Multiplication happens before addition, just like in math class. Parentheses change the order: (2 + 3) * 4 is 20.
The problem is that code has more operators than school did. And not every programmer remembers where &&, || and ?? rank. The rule is simple: when in doubt, add parentheses. They cost nothing, and the code becomes far easier to read — for you and for the AI that will edit it.
Why this matters if AI writes your code
Operators are the smallest bricks of a program. A model places them correctly almost always. Almost. The bugs hide at the edges: negative numbers, text instead of a number, an empty value.
When something is "sometimes" wrong, look for an operator at an edge. Check three things:
- What's on either side — numbers or strings?
- Could the value be negative or zero?
- In what order will this be calculated without parentheses?
And you can add one sentence to your prompt: "check the edge cases — the first item, zero, a negative number". That's enough for the model to find the % trap on its own.
How is an operator different from a function?
In meaning — it isn't: both take values and return a result. The difference is how they're written: an operator is a symbol between values (a + b), a function is a name with parentheses (Math.max(a, b)).
Why are = and === different things?
A single = puts a value into a variable, while === compares two values. Mixing them up in a condition is a classic bug: if (x = 5) doesn't check anything, it changes x.
What does ++ after a variable mean?
i++ means add one, the same as i += 1. You'll mostly see it in counters and loops.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





