Regarding JavaScript for() loop voodoo

前端 未结 9 1419
梦如初夏
梦如初夏 2021-02-19 08:53

I was for quite some time under the impression that a for loop could exist solely in the following format:

for (INITIALIZER; STOP CONDITION         


        
相关标签:
9条回答
  • 2021-02-19 09:50

    Syntax of for loop is:

    for (pre-block; condition; post-loop-block)
        loop-block;
    

    First, pre-block is executed, various variables are defined.

    In each loop:

    1. check condition
    2. execute loop-block
    3. execute post-loop-block
    4. repeat from 1.
    0 讨论(0)
  • 2021-02-19 09:50

    this goes all the way back to C syntax - from which javascript has stole a bunch. the main trick is the comma-operator which seems to appear in almost no other place except for loops

    0 讨论(0)
  • 2021-02-19 09:52

    The first clause initializes any variables you want to use. The second clause is indeed the stop condition. The third clause includes any logic to be executed at the end of each iteration. Multiple statements can be separated by commas.

    0 讨论(0)
提交回复
热议问题