Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is an old-ish answer. I wrote a <a href="https://stackoverflow.com/questions/12272836/const-keyword-scope-in-javascript/12272877#12272877">slightly newer answer</a> dealing with similar "const re-assignment/scope" issues where I show that the errors produced, if any, vary by execution method <em>and</em> browser. </p> <p>Since <code>const</code> (which is not part of the ECMAScript 5th edition standard) is slated to have a <em>different</em> meaning in ECMAScript 6th edition, I would recommend avoiding it in current code.</p> <hr> <p><code>const</code>, like <code>var</code> is "function-scoped". I <em>suspect</em> the problem arises from the same kind of "to top of function" promotion on the <em>binding</em> that happens with <code>var</code> (this explains why the exception does not come from the assignment but from the declaration). That is, any subsequent <code>const x = ...</code>, <em>wherever they appear</em>, are considered to be invalid because a previous declaration already exists (by definition, there can only be <em>one const of a given name per scope</em>). However, <code>const</code> can take on any value so the <em>assignment</em> happens at the <code>const x = ...</code> site, just as the assignment occurs at the <code>var x = ...</code> site, even though the annotation/binding was promoted to the top of the scope.</p> <p>Here is a simple test-case which demonstrates the problem more clearly:</p> <pre><code>function x () { if (true) { const a = 1 } else { const a = 2 }} // =&gt; TypeError: redeclaration of const a @ &lt;x-jsd:interactive-session </code></pre> <p>As you can see, the <em>error occurs at the function declaration</em> and <em>not</em> at the function execution. This is why the try/catch does not work. The behavior may also be influenced by the interactive tool you are dealing with depending on how it executes the code (e.g. it's the same execution context each time?).</p> <p>However, this works fine and reinforces the initial proposition above:</p> <pre><code>(function x() { if (false) { const c = 1 }; return c })() // =&gt; undefined </code></pre> <p>From <a href="https://developer.mozilla.org/en/JavaScript/Reference/Statements/const" rel="nofollow noreferrer">https://developer.mozilla.org/en/JavaScript/Reference/Statements/const</a></p> <p>(Bold Emphasis added)</p> <blockquote> <blockquote> <p>Creates a constant that can be global or local to the function in which it is declared. <strong>Constants follow the same scope rules as variables.</strong></p> <p>The value of a constant cannot change through re-assignment, and a constant cannot be re-declared. Because of this, although it is possible to declare a constant without initializing it, it would be useless to do so.</p> <p>A constant cannot share its name with a function or a variable in the same scope.</p> <p><strong>const is a Mozilla-specific extension</strong>, it is not supported by IE, but has been partially supported by Opera since version 9.0 and Safari.</p> </blockquote> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload