jeudi 27 octobre 2016

Using an if statement with a let/const statement, without a block statement

In JavaScript, I can declare a variable using var in an if statement, without a block statement:

if (true)
  var theAnswer = 42

However, trying to declare a variable using let or const without a block statement gives an error:

if (true)
  let theAnswer = 42

Chrome throws SyntaxError: Unexpected identifier, Firefox—SyntaxError: lexical declaration not directly within block.

if (true)
  const theAnswer = 42

Here Chrome throws SyntaxError: Unexpected token const, Firefox—SyntaxError: const declaration not directly within block.

What is the reason of that? Is there anything in the specification that would explain this behavior?

Aucun commentaire:

Enregistrer un commentaire