restrictions on let declarations

Allen Wirfs-Brock allen at wirfs-brock.com
Thu Jan 30 08:05:14 PST 2014


On Jan 30, 2014, at 7:43 AM, John Lenz wrote:

> It seems unfortunate that "let" and "const" have different usage rules from "var".  It seem strange  that "var" is considered a statement and not declaration as per:
> 
> http://people.mozilla.org/~jorendorff/es6-draft.html#sec-ecmascript-language-statements-and-declarations
> 
> Generally, I've always thought of:
> 
> "if (x) ..." as equivalent to "if (x) { ... }"
> 
> Does this restriction on let/const enable anything?
> 

The anomaly is that 'var' was considered a statement in ES1. But note that the only other declaration in the language ('function') at that time was not a statement. See http://www.wirfs-brock.com/allen/draft-ES5.1/#sec-14 

Declarations have global static impact within their containing scope (function or block). It generally doesn't make any sense to use them in a conditional context without an explicitly surrounding block that constrains their scope.

So all new declarative forms in ES6 are treated similarly to function.  They are declarations but not statements.  Var must remain a statement for legacy compatability.

Allen




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140130/09766f21/attachment.html>


More information about the es-discuss mailing list