Skip to content

Instantly share code, notes, and snippets.

@KLagdani
KLagdani / hoisting-function.js
Created May 6, 2020 11:51
Hoisting for function
//sayHi is hoisted during the creation of the context execution
sayHi("Bulma"); //calls the function and prints "Bulma says hi"
function sayHi(who) {
console.log(who + " says hi");
}
@KLagdani
KLagdani / hoisting-const.js
Created May 6, 2020 11:50
Hoisting for const declaration
//myConst is hoisted during the creation of the context execution
//It exists in memory with undefined as a value
//But we are not allowed to access it before declaring it
myConst = "I am a const"; //Throws: Cannot access 'myConst' before initialization
console.log(myConst); //line not reached due to error thrown above
const myConst = "Initializer";
@KLagdani
KLagdani / hoisting-let.js
Last active May 20, 2020 09:45
Hoisting for let declaration
//myLet is hoisted during the creation of the context execution
//It exists in memory with undefined as a value
//But we are not allowed to access it before declaring it
myLet = "I am a let"; //Throws: Cannot access 'myLet' before initialization
console.log(myLet);
let myLet;
@KLagdani
KLagdani / hoisting-var.js
Last active May 6, 2020 11:48
Hoisting for var declaration
//myVar is hoisted during the creation of the context execution
//It exists in memory with undefined as a value
myVar = "I am a var"; //Perfectly ok
console.log(myVar); //Prints "I am a var"
var myVar;
//OR
console.log(myVar); //Perfectly ok, prints "undefined"
var myVar;