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
in es6 when we use let or const we have to declare the variable before using them. eg. 1 -
// this will work
u = 10;
var u;
// this will give an error
k = 10;
let k; // ReferenceError: Cannot access 'k' before initialization.
eg. 2-
// this code works as variable j is declared before it is used.
function doSmth() {
j = 9;
}
let j;
doSmth();
console.log(j); // 9