0

Hi javascript learners why this short code prints such strange value.

for(i=1; i<10; i+=3){} console.log(i);//output = 10 for(i=1; i<10; i+=5){} console.log(i);//output = 11

11th Aug 2020, 3:27 PM
Abdihakim Ismail
Abdihakim Ismail - avatar
3 Antworten
+ 4
Hey, the thing is, you should use let or var here. for(let i = 1; i < 10; i += 3) { // Now i is only accessible inside these curly braces. The output was strange coz you were making a global variable, without the keyword let or var. }
11th Aug 2020, 3:50 PM
maf
maf - avatar
+ 3
for(i=1;i<10;i+=5{} is the same as i=1 while(i<10) {i+=5} so lets look. At first i is 1 and therefore smaller 10. Then i is 6 and still smaller 10. Then i is 11 and not smaller 10 anymore. While-loop ends with i =11 So, where is is strange?
11th Aug 2020, 3:36 PM
Alexander Thiem
Alexander Thiem - avatar
+ 2
Thank you all i just got what was wrong with me, simple, I put it outside the curly braces, while i wanted to print i loops, I've been doing and learning javascript for few months
11th Aug 2020, 4:12 PM
Abdihakim Ismail
Abdihakim Ismail - avatar