What does mean for loop with one semicolon?

96 views Asked by At

Can please someone give an example of third for loop from specification and explain what does it mean?

14.7.4 The for Statement

  • Syntax

    • ForStatement[Yield, Await, Return] :

      • for ( [lookahead ≠ let [] Expression[~In, ?Yield, ?Await] opt ; Expression[+In, ?Yield, ?Await] opt ; Expression[+In, ?Yield, ?Await] opt ) Statement[?Yield, ?Await, ?Return]

      • for ( var VariableDeclarationList[~In, ?Yield, ?Await] ; Expression[+In, ?Yield, ?Await] opt ; Expression[+In, ?Yield, ?Await] opt ) Statement[?Yield, ?Await, ?Return]

      • for ( LexicalDeclaration[~In, ?Yield, ?Await] Expression[+In, ?Yield, ?Await] opt ; Expression[+In, ?Yield, ?Await] opt ) Statement[?Yield, ?Await, ?Return]

2

There are 2 answers

0
Jasmijn On BEST ANSWER

If you look at the definition of LexicalDeclaration, you can see it is defined as:

  • LexicalDeclaration[In, Yield, Await] :
    • LetOrConst BindingList[?In, ?Yield, ?Await] ;

So all three cases of for loops have two semicolons. Lexical declarations are not expressions and so they have chosen to include the semicolon in its definition, rather than following every use of LexicalDeclaration with ;.

2
Hien Hoang On

It should be something like this:

for (let i = 0; i < 5; i++) {
    console.log(i);
}
  • let i = 0; is the Lexical Declaration
  • i < 5 is the condition checked before each iteration
  • i++ is executed after each loop iteration