In JavaScript what does for(;;){…} do?

前端 未结 5 1104
感动是毒
感动是毒 2021-01-26 08:40

I\'ve seen this in some JavaScript code but I don\'t get what it does.

for(;;){

  //other code

}

I\'m used to the for(i=0;i

5条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-26 09:32

    It creates a loop that runs indefinitely. It's the same syntax you're used to seeing, there's just no code between the semicolons.

    The syntax for a for loop includes an initializer, followed by a semicolon, followed by a condition for continuing the loop, followed by a semicolon, followed by code to run after each iteration of the loop.

    Since there is no initializer, no condition that ever evaluates to false, and no post-loop code, the loop runs forever.

提交回复
热议问题