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
4 ответов
+ 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.
}
+ 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?
+ 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