Pre-increment
See the example below: in the pre increment, first the variable c
is incremented, and only then assigned to d
:
var c, d;
c=6;
console.log("Pre-incremento\n");
console.log("Numero sem incremento: %d\n", c); // 6
d=++c; // O VALOR É INCREMENTADO, E SÓ DEPOIS PASSADO PARA 'd'
console.log("Valor de 'c':%d\nValor de 'd':%d\n", c, d); // c = 7, d = 7
In this example, c
, that is worth 6 is first incremented and becomes valued 7. Only after that, the variable - already counting 7 - is assigned to 'd', which is also worth 7. >
Post-Increment
See in the example that the variable is first assigned, and only then incremented:
var c, d;
c=6;
console.log("Pos-incremento\n");
console.log("Numero sem incremento: %d\n", c); // 6
d=c++;// O VALOR É PASSADO PARA 'd', E DEPOIS INCREMENTADO
console.log("Valor de 'c':%d\nValor de 'd':%d\n", c, d); // c = 7, d = 6
In this example, c
, which is worth 6 has its value assigned to d
, which is worth 6 as well. Only after this operation does c
have its value incremented, then it is 7.
The same rule applies to decrements
Pre-decrement
var c, d;
c=6;
console.log("Pre-decremento");
console.log("Numero sem incremento: %d", c); // 6
d=--c; // O VALOR É DECREMENTADO, E SÓ DEPOIS PASSADO PARA 'd'
console.log("Valor de 'c':%d\nValor de 'd':%d\n", c, d); // c = 5, d = 5
Post-decrement
var c, d;
c=6;
console.log("Pos-decremento");
console.log("Numero sem incremento: %d", c); // 6
d=c--; // O VALOR É PASSADO PARA 'd', E DEPOIS DECREMENTADO
console.log("Valor de 'c':%d\nValor de 'd':%d\n", c, d); // c = 5, d = 6
Therefore
In your pre or post increment example will always result in the same result , since there is no assignment of the value of i
to another variable, you are just returning the value of i
after the increment operation, be it pre or post.
In this way your example works just like:
c = 6;
c++; // o valor de 'c' é 7
console.log(c); // retornará 7
or
c = 6;
++c; // o valor de 'c' é 7
console.log(c); // retornará 7