Namaste JavaScript — Interview Quick Notes
Ep 3: Hoisting
- Hoisting enables extracting values of variables and functions before initialising/assigning — due to the Memory Creation Phase of EC.
- var declarations are hoisted and initialised to undefined.
- Function declarations are hoisted with their entire body — callable before the line they appear.
- let/const are hoisted but stay in the Temporal Dead Zone — accessing them before initialisation throws ReferenceError.
- var fn = function() {} — fn is hoisted as undefined; calling fn() before this line → TypeError.
1getName() // "Namaste JS" ← works, function hoisted fully
2console.log(x) // undefined ← var hoisted as undefined
3var x = 7
4function getName() {
5 console.log("Namaste JS")
6}
7
8var fn = function () {} // fn is undefined before this line