Hoisting clarification

Viewed 62

Javascript engine executes in two phases: creation phase and execution phase. During the creation phase, variables are allocated memory. Here, allocated memory means that var variables are declared and initialized with undefined whereas let and const variables are declared but are uninitialized and inaccessible until the code executes the declaration of these variables during the execution phase. So, my understanding is that hoisting refers to the allocation of memory during the creation phase.

console.log(val1) //undefined
var val1 = 6;

console.log(val2) //error with variable in temporal dead zone
let val2 = 6;

let val3; 
console.log(val3) //undefined
val3 = 6;

MDN states “the variable and function declarations are put into memory during the compile phase but stay exactly where you typed them in your code”. Is compile phase different from creation phase? And is my understanding of how hoisting works correct?

0 Answers
Related