+ 3

Why's this 04 & not 02?

for(i = 0; i < 5; i++) { i += i; document.write(i); i++; }

19th Feb 2018, 7:22 PM
Briaה‎
2 Respuestas
+ 5
i += i means i = i+i i++ mean increase by 1 So, 1st time: i = 0+0 write 0. i become 1. Then increase by 1 (which is in for loop). so i become 2 (condition is still true). So, 2nd time: i = 2 + 2 write 4. i become 5. Then increase by 1 in for loop. So i become 6. Then condition is false. Output is 04
19th Feb 2018, 7:41 PM
Sylar
+ 3
In the for loop, it will automatically increment the variable being tested within the brackets, based on the third condition given (in this case it bring i++). Because you're incrementing i twice (once within the loop and once for the actual loop), the second iteration returns i as 2, which gets added to itself, therefore resulting in it being 4.
19th Feb 2018, 7:38 PM
Faisal
Faisal - avatar