Why does a for loop with a semicolon after it still execute?

前端 未结 4 1603
离开以前
离开以前 2020-12-21 13:42

I was having a look at some code a friend did and stumbled across this line which I thought was an error (simplified for example)..

for (g = 0; 10 > g; g+         


        
4条回答
  •  一生所求
    2020-12-21 14:26

    Your g variable is within the global context, so it is accessible outside of a for loop. The loop did its job, and incremented g 10 times.
    Two {} indicate a block in JavaScript and won't cause any error.

    Edit: it is not alerting anything without a for loop, because the g variable is not defined.

提交回复
热议问题