Clarification on let/const

Nicholas C. Zakas standards at
Tue Jul 31 11:00:34 PDT 2012


I've been going through the spec trying to figure out a few edge cases 
for block bindings. It appears that there is not a functioning demo for 
these (Traceur is mostly working with var semantics, Firefox/Chrome are 
not at all spec-compliant), so just thought I would ask to get a better 
understanding of the expected behavior.

1. If I'm interpreting this correctly, let and const declarations will 
throw an error if used with an identifier that is already defined within 
the same block. So I think that means this should throw an error:

    function f() {
         var count = 10;
         let count = 20;   // error?

However, it seems like this would not (block variable shadowing function 

    function f() {
         var count = 10;

         if (condition) {
             let count = 20;   // no error?

And I would assume this is the same (using let instead of var):

    function f() {
         let count = 10;

         if (condition) {
             let count = 20;   // no error?

Am I correct in my understanding of these?

2. Since ECMAScript 6 is always running in strict mode, I'm assuming 
that attempting to assign to a declared constant would throw an error 
(in that it behaves similar to a non-writable property in strict mode)?

    const MAX_ITEMS = 30;

    MAX_ITEMS = 25;    // error?

Is that correct?


Nicholas C. Zakas

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list