DEV Community

Azaan Suhail
Azaan Suhail

Posted on

Day 5 of Complete JavaScript in 17 days | Visual Series📚✨

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!");
}
Enter fullscreen mode Exit fullscreen mode

🔹 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!");
};
Enter fullscreen mode Exit fullscreen mode

🔹 var?

Hoisted, but value is undefined

console.log(x); // undefined
var x = 10;
Enter fullscreen mode Exit fullscreen mode

🔹 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;
Enter fullscreen mode Exit fullscreen mode

🎯 Summary:

)> Except Arrow functions all normal function show the hoisting nature.
)> Var is hoisted , let & const are also hoisted only in the temporal deadzone.

Top comments (0)