Hoisting behaviour of 'const' and 'let'

Peter Michaux petermichaux at gmail.com
Sun Oct 12 12:17:24 PDT 2008


On Sun, Oct 12, 2008 at 12:11 PM, David-Sarah Hopwood
<david.hopwood at industrial-designers.co.uk> wrote:
> Peter Michaux wrote:
>> Hi David,
>>
>> offlist as I haven't been following this closely enough...
>
> I prefer to discuss on the list, for future reference.
>
>>>>>    * it doesn't need to hoist for backward compatibility, unlike 'var';
>>>> Not for compatibility with the standard, but const as implemented in
>>>> at least 2/4 (not sure about JSCore) hoists, I believe.
>>> You mean code like this?
>>>
>>> (function () {
>>>  print(x);
>>>  { const x = 42; }
>>> })();
>>>
>>> (calls 'print(undefined)').
>>
>> The expected output should be "undefined", shouldn't it?
>
> For some definition of "expected". That's what it does in FF3 (I don't
> have Safari or Opera installed, and IE7 throws a SyntaxError), but I
> don't think it is expected, or useful, that a const variable can
> effectively be mutable. For 'var' there are potential uses of hoisting
> (even if they would be more clearly expressed in other ways), but not
> really for 'const'.

Either const should not hoist, the print above should show
"undefined", or an error is thrown "using a const not yet assigned."

It would be completely unintelligible to read code where the print
above shows "42".

Peter


More information about the Es-discuss mailing list