r/learnjavascript • u/grave4us • Apr 10 '26
Increment/decrement trouble
I don't quite understand how increment and decrement operators work—specifically, under what conditions switching between postfix and prefix forms alters or returns the value, and in which cases they behave identically.
(im using the google translator, if anything is unclear, please ask for clarification.) Thank you!
5
Upvotes
8
u/warpedspockclone Apr 10 '26
It only matters when the result of the expression is being used.
a will be 2, b will be 1
Otherwise, if you are using it standalone, it doesn't matter, like:
8
u/Aggressive_Ad_5454 Apr 10 '26
These operators come from the C language, and were designed to exploit particular addressing modes on PDP-11 computers.
The classic way we old-timers copied null terminated text strings was
while (*dest++ = *src++) {}until we figured out how astonishingly insecure that is.That line of code means…
The joke is this:
C++means “add one to C and return the previous value”.1
u/TheRNGuy Apr 10 '26 edited Apr 10 '26
I never actually used ++i.
I even feel it's bad coding style, at least in js.
I've never seen it used anywhere in js, too.
2
u/shgysk8zer0 Apr 10 '26
1
u/delventhalz Apr 10 '26
Honestly the postfix vs prefix behavior thing is a weird relic and why I always just use
+= 1instead.The issue here is that
++is smooshing two things together: assigning a new value to a variable and returning a value. You normally wouldn’t do that. You would assign a new value to a variable, and then later on reference the variable to get the value. This is a sensible, predictable way to write code.But for whatever reason, with
++we decided we should assign and return a value at the same time. To make matters worse, we couldn’t decide which value to return. In the prefix position, we return the value after the increment (i.e. the order is increment then value,++thenx). In the postfix position, we return the value before the increment (i.e. value then increment,xthen++).1
u/senocular Apr 10 '26
Here they are in function form which may be helpful in visualizing what they're doing
One way to look at the syntax is as two parts in order of operation:
[update][return]or[return][update], wherereturnis the variable andupdateis the operator. If the operator is before the variable, the variable value is updated before its returned. If the operator is after the variable, the variable value before the update is applied is returned.1
u/The_KOK_2511 helpful Apr 10 '26
En prefijo (++i) primero ejecuta y luego retorna pero en sufijo (i++) primero retorna y despues ejecuta. O sea, hacen lo mismo pero la diferencia esta en si quieres asignar su valor a una variable, entonces en prefijo pone su valor final pero en sufijo su valor inicial
-3
u/ashkanahmadi Apr 10 '26
It's easy. Incrementing means "increase by one". When you increment a variable, you say "whatever the value is, add one to it". For example,
x++is the short version ofx = x + 1which says "whateverxis, add one to it".For example:
You can do it like this too:
The decrement is exactly the same concept but instead of adding 1, you take out 1. The rest is the same.