Block Scope III


#1

This code

const starCount = () => {
  let i = 5;
  console.log(i);
  
  for(i = 0; i < 12; i++) {
    console.log(i);
  }
};

starCount();

returns the following output -

5
0
1
2
3
4
5
6
7
8
9
10
11

My question is, why after printing 5 it started from 0 instead of 6?


#2

because in the loop, you re-assign i to 0:

for(i = 0

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.