Hoisting behaviour of 'const' and 'let'

David-Sarah Hopwood david.hopwood at industrial-designers.co.uk
Sun Oct 12 12:11:42 PDT 2008


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'.

-- 
David-Sarah Hopwood



More information about the Es-discuss mailing list