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

前端 未结 4 1613
离开以前
离开以前 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:14

    As far as I know this is simply a syntax feature of the language. Basically, javascript ignores that semicolon.

    EDIT: Sorry, misread your post. If you mean that you only get alerted once, then you have basically an empty loop executing ten times, then a single block of code containing the alert executing once.

    Accolades ({...}) can go around any block of code, independent of constructs such as for-loops.

提交回复
热议问题