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?