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

前端 未结 5 1002
感动是毒
感动是毒 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:22

    That's an infinite loop. A for loop has 3 sections separated by ;: the initialization section where you normally declare and define your counter's starting conditions, the middle conditional statement that evaluates to true or false, and the increment section.

    All 3 of these sections are optional. You can include any or none. The loop continues to loop until the conditional is false. If that condition is never met (since it was left out), it loops forever.

    A place where you'll likely see it is prepended to JSON data as described by this SO post.

提交回复
热议问题