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.



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *