+ 2
In javascript, ++i does what? See example
function sum(i) { return ++i; }
4 Réponses
+ 7
// Increment operators
x = 1;
y = ++x; // x is now 2, y is also 2
y = x++; // x is now 3, y is 2
// Decrement operators
x = 3;
y = x--; // x is now 2, y is 3
y = --x; // x is now 1, y is also 1
+ 2
ok, but...
function sum(i) {
return ++i;
}
for (var i = 0; i <= 1; i++) {}
document.write(sum(i));
returns 3. why? was my next question.
but i just realized:
the for loop runs twice, this sets i to 2.
then sum(i) increases it to 3.
thanks for all the answers.
+ 1
The ++ before the i is used to increase i by one (it can be also after the i like i++)