Does a const have block scope?

A test to see if the browsers use block scope for 'const' or treat it like 'var'.

by David Storey

HTML

<p>Open the Console in your browser. The value posted to the console should initially be "Foo". The second time we print to the console, CONSTANT_VARIABLE should be out of scope, as <code>const</code> has block level scoping. If the "Foo" is posted again to the console then the browser doesn’t not use block level scoping for <code>const</code>.</p>

JavaScript

var i = true;

if (i) {
    const CONSTANT_VARIABLE = "Foo";

    /* should print "Foo" to console */
    printValue(CONSTANT_VARIABLE);
}

/* CONSTANT_VARIABLE should not exist */
printValue(CONSTANT_VARIABLE);

function printValue(val) {
    console.log("Value of CONSTANT_VARIABLE is " + val);
}