Day 5 of My JavaScript Visual Series šāØ
š” Hoisting in JavaScript ā JavaScript ka jugaadu nature š
Real life example : –
Imagine youāre in class and the teacher asks a questionā¦
You didnāt study, but your bestie already gave your name to answer ā JavaScript does the same thing š
- It remembers your function or variable declarations even before they are actually written ā thatās Hoisting.
š Real-Life in Code:
š¹ Function Hoisting
You can call the function before declaring it.
greet();
function greet() {
console.log("Good to see you!");
}
š¹ Anonymous Function / Arrow Function?
No shortcut! These wonāt work if you call them before defining.
sayHi(); // ā Error
var sayHi = function() {
console.log("Hey there!");
};
š¹ var?
Hoisted, but value is undefined
console.log(x); // undefined
var x = 10;
š¹ let & const? [Interview Question]
JavaScriptā: āI know it exists⦠but you can’t touch it yet š¶ā
They live in a Temporal Dead Zone (TDZ).
console.log(y); // ā ReferenceError
let y = 5;
šÆ Summary:
)> Except Arrow functions all normal function show the hoisting nature.
)> Var is hoisted , let & const are also hoisted only in the temporal deadzone.