Hoisting - var vs let
Video
JavaScript Notes
JavaScript
/**
* Hoisting
* var vs let (and const)
*/
let log = console.log;
f(); //undefined function
log(fe); //ReferenceError - because we are inside the Temporal DeadZone, as we used let fe
fe(); //returns an error as the function hasn't yet been assigned to fe
log(v1); //undefined variable
var v1;
log(L1); //ReferenceError - because we are inside the Temporal DeadZone
let L1; //did get hoisted, but the 'undefined' assignment happens at this line
log(L1);
function f() {
//function declaration
//gets hoisted to the top just like var
log("function f");
let x; //to avoid global var called x
x = "hello"; //global var created called x - bad coding practice, if not declared first using let/var x;
}
// function expression - assignment to a variable
// fe gets hoisted to the top, but function assignment happens here
let fe = function e() {
log("function fe");
};
Feedback
Submit and view feedback