Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

let in if without braces

if (true) {
  let x = 5
}

works as expected (no syntax error), but

if (true) let x = 5

throws SyntaxError: Unexpected strict mode reserved word in Node 4.1.0 and babel

Is this expected behavior? I know that this is a stupid example. I'm just wondering wether this is a bug or not.

like image 234
implicit_knowledge Avatar asked Dec 25 '22 14:12

implicit_knowledge


1 Answers

Yes it is expected behavior. The production rule of an if statement is

 if ( Expression[In, ?Yield] ) Statement[?Yield, ?Return]

but a let declaration is not a Statement and is therefore not allowed in this position:

Statement[Yield, Return] :
    BlockStatement[?Yield, ?Return]
    VariableStatement[?Yield]
    EmptyStatement
    ExpressionStatement[?Yield]
    IfStatement[?Yield, ?Return]
    BreakableStatement[?Yield, ?Return]
    ContinueStatement[?Yield]
    BreakStatement[?Yield]
    [+Return] ReturnStatement[?Yield]
    WithStatement[?Yield, ?Return]
    LabelledStatement[?Yield, ?Return]
    ThrowStatement[?Yield]
    TryStatement[?Yield, ?Return]
    DebuggerStatement

Declaration[Yield] :
    HoistableDeclaration[?Yield]
    ClassDeclaration[?Yield]
    LexicalDeclaration[In, ?Yield]

LexicalDeclaration[In, Yield] :
    LetOrConst BindingList[?In, ?Yield] ;
like image 141
Felix Kling Avatar answered Jan 13 '23 18:01

Felix Kling