Luboš Turek Luboš Turek - 4 months ago 51
Javascript Question

Are variables declared with let or const not hoisted in ES6?

I have been playing with ES6 for a while and I noticed that while variables declared with

var
are hoisted as expected...

console.log(typeof name); // undefined
var name = "John";


...variables declared with
let
or
const
seem to have some problems with hoisting:

console.log(typeof name); // ReferenceError
let name = "John";


and

console.log(typeof name); // ReferenceError
const name = "John";


Does this mean that variables declared with
let
or
const
are not hoisted? What is really going on here? Is there any difference between
let
and
const
in this matter?

Answer

@thefourtheye is correct in saying that these variables cannot be accessed before they are declared. However, it's a bit more complicated than that.

Are variables declared with let or const not hoisted? What is really going on here?

All declarations (var, let, const, function, function*, class) are hoisted in JavaScript. This means that if a name is declared in a scope, in that scope the identifier will always reference that particular variable:

x = "global";
(function() {
    x; // not "global"

    var/let/… x;
}());
{
    x; // not "global"

    let/const/… x;
}

This is true both for function and block scopes1.

The difference between var/function/function* declarations and let/const/class declara­tions is the initialisation.
The former are initialised with undefined or the (generator) function right when the binding is created at the top of the scope. The lexically declared variables however stay uninitialised. This means that a ReferenceError exception is thrown when you try to access it. It will only get initialised when the let/const/class statement is evaluated, everything above that is called the temporal dead zone.

x = y = "global";
(function() {
    x; // undefined
    y; // Reference error: y is not defined

    var x = "local";
    let y = "local";
}());

Notice that a let y; statement initialises the variable with undefined like let y = undefined; would have.

Is there any difference between let and const in this matter?

No, they work the same as far as hoisting is regarded. The only difference between them is that a constant must be and can only be assigned in the initialiser part of the declaration (const one = 1;, both const one; and later reassignments like one = 2 are invalid).

1: var declarations are still working only on the function level, of course