?? JavaScript Odyssey: Hoisting, Temporal Dead Zone, and Lexical Environment Adventure! ???


Ahoy, fellow coders! ?? Ready for a captivating journey into the depths of JavaScript mysteries? Let's unravel the enigma of hoisting, explore the Temporal Dead Zone (TDZ), and understand the silent observer, Lexical Environment. ???


Illuminating the Mysteries:

??? Temporal Dead Zone (TDZ) - The Quantum Leap:

Did you know that during the TDZ, JavaScript creates a quantum state for our variables? They exist, yet are unobservant and inaccessible until the magical declaration moment arrives. ??

console.log(x); // Error: Cannot access 'x' before initialisation
let x = 10;        


?? Lexical Environment - The Silent Observer:

Meet Lexical Environment, the watchful guardian of scopes. It keeps track of variables and their values, defining the rules of engagement within the code. ???♂???

function outerFunction() {
   let outerVar = "I'm in the outer realm!";

   function innerFunction() {
      console.log(outerVar); //Accessing outerVar from innerFunction
   }

   innerFunction();
}

outerFunction();        


The Chronicles of Hoisting:

Fact 1 - Hoisting Honors (var):

Did you know that var is hoisted with both declaration and initialisation? It's like a time traveller, showing up at the beginning of its scope with a note from the future! ????

console.log(a); // undefined
var a = 5;        


Fact 2 - The Dance of let:

Now, here's the twist! let gracefully hoists the declaration but withholds its value until later. It's like a ballet dancer, leaving a placeholder until the grand performance begins! ????

console.log(b); // Error: Cannot access 'b' before initialisation
let b = 10;        


Fact 3 - const: A Fortress of Immutability:

And behold, const stands strong, declaring its kingdom with the hoisted declaration. Once initialised, it becomes an immovable fortress—immutable and resolute! ?????

console.log(c); // Error: Cannot access 'c' before initialisation
const c = 42;        


?? Trivia Quest - Into the Unknown:

Did you know that trying to access a variable within the Temporal Dead Zone might reveal a different result than you'd expect? ?? What's your prediction?

Sarfraj Ansari

web 3.0 | Javascript | Typescript | React Js | Redux | RTK Query | Next.js | Node Js | Express js | Nest js | MongoDB | Sequelize | Docker

1 年

Thanks for sharing

Sumit Kumar

Student at Rashtriya Military School,Chail

1 年

Oops... I didn't knew any of these

要查看或添加评论,请登录

Amit Sheoran的更多文章

社区洞察

其他会员也浏览了