Hoisting behaviour of 'const' and 'let'

Brendan Eich brendan at mozilla.com
Mon Oct 13 15:43:47 PDT 2008


On Oct 13, 2008, at 2:40 PM, Brendan Eich wrote:

> If we don't require let initializers then users will sometimes forget
> them, and use-before-set errors

s/errors/mistakes/

> will happen -- unless we require
> analysis or a read barrier to make use-before-set an error. If we do
> not mandate analysis, then test coverage gaps will leave some such
> errors to be found in the field.

I hope this was clear enough the first time, but in case not, my  
argument is that let should not require an initializer because var  
does not, and we wish to migrate code using var to use let, and  
encourage let instead of var in new code. Requiring an initializer is  
unnecessary, and it taxes all programers whether trying to migrate old  
code they didn't write, use let as they're accustomed to using var in  
new code, or something in between.

Anyway, that's all about let. For const, which requires an  
initializer, everyone agrees that use before initialization is an  
error. Whether it must be a static error is not yet decided by the  
committee, and the latest ES3.1 draft does not say more than "Any  
attempts to access the value of a constant before it is assigned a  
value throws a ReferenceError exception." (Ugly wording there, I'll  
help get it fixed.)

And ES3.1 specifies const hoisting to top of block, so you cannot use  
an outer x in the first ... in { ... const x = 42; ... }.

/be


More information about the Es-discuss mailing list