on my quest to learn and improve my JavaScript I came across a script that has a switch / case statement and I noticed some variables are incremented using ++ with the variable before the ++ and then some variables have the ++ after the variable. What’s the difference between these? Here’s an example of what I’m trying to explain notice the m and y variables.
switch(f){
case 0:{
++m;
if(m==12){
m=0;
y++;
}
break;
}
case 1:{
--m;
if(m==-1){
m=11;
y--;
}
break;
}
case 2:{
++y;
break;
}
case 3:{
--y;
break;
}
case 4:{
break;
}
}
++ireturns the value ofiafter it has been incremented.i++returns the value ofibefore incrementing.When the
++comes before its operand it is called the “pre-increment” operator, and when it comes after it is called the “post-increment” operator.This distinction is only important if you do something with the result.
One thing to note though is that even though
i++returns the value before incrementing, it still returns the value after it has been converted to a number.So