In javascript, ++i does what? See example | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

In javascript, ++i does what? See example

function sum(i) { return ++i; }

6th Aug 2017, 1:52 PM
Rcknmrty
Rcknmrty - avatar
4 Answers
+ 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
6th Aug 2017, 2:01 PM
The Coding Sloth
The Coding Sloth - avatar
6th Aug 2017, 2:20 PM
Hatsy Rei
Hatsy Rei - avatar
+ 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.
6th Aug 2017, 4:04 PM
Rcknmrty
Rcknmrty - avatar
+ 1
The ++ before the i is used to increase i by one (it can be also after the i like i++)
6th Aug 2017, 1:57 PM
Αητοιπe
Αητοιπe - avatar