- 1
JavaScript loops
What is the difference between the two codes? for(i = 0;i < 5;i++){ i += i; Document.write(i); } And for(i = 0;i < 5;i++){ Document.write(i); i += i; } and i don't understand it ( i+=i) and outputs
4 Respuestas
+ 2
i += i means i = i + i. Basically when you have, in more general terms: x += y it means x = x + y
Now, I have explained what is going on with the two loops in the comments in this code. Go to the JS tab. You can also run it and see the outputs in the console
Chess Check this out.
https://code.sololearn.com/WN88XktetWc7/?ref=app
+ 2
i+=i means i is incrementing with i
i=5
i+=i means i+=5 so i=10
and for this two codes the second justs outputs i's value before the incrementation so different outputs
0
i+=i means i is incremented by i. That means i = i + i
In the 1st case, first i is incremented by i i.e. becomes 2i and then print the modified i.
2nd case, first i is printed and then modified i.e. increasing by i.
0
thank you for your answers but i don't understand what is the output and why ?