DEV Community

Discussion on: Advanced Usage of Styled Components for your React App - Part 1

Collapse
 
olenadrugalya profile image
Olena Drugalya

its because you re-defined var i in your loop again, so the loop uses var i = 0. If you want var i to start from 4, you should do the following:
for(var i = 4 ; i<10;i++)
OR
for(var i = i ; i<10;i++)

Collapse
 
sandeep18072002 profile image
Sandeep18072002

Bro I don't want to start my var from 4 I am saying that if-

var i = 4; // stored in globals

for(var i = 0; i<10; i++){
console.log(i); // the var is block scoped and and it's 0 it will iterate until i<10 which is just obvious but after the loop / The second console.log(i) ; refers to globals but it gives 10 ; instead of 4 or 9+4 = 13 ;; CONCERN is - why 10 [from where is became 10]
}
console.log(i); // 10 ??????????

Thread Thread
 
sivaprasadraj profile image
Siva Prasad

var doesn't create a block scope for the loop, it just rewrites the value for the variable i. Since the value of i changes to 10 after the loop ends, that will be printed. If you want to create a block scope for the loop, use let.