DEV Community

Cover image for πŸͺ„ Hoisting in JavaScript: A Tale of Misplaced Trust
Jagroop Singh
Jagroop Singh

Posted on

πŸͺ„ Hoisting in JavaScript: A Tale of Misplaced Trust

Once upon a time in the land of Codeville, there lived two friends, Jagroop πŸ§‘β€πŸ’» and Manish πŸ‘©β€πŸ’». Both were JavaScript developers, but they had very different styles of declaring variables and functions. One day, they embarked on a journey to debug an issue with their magical spells... err, code.

The Mysterious Bug πŸ€”

Jagroop had written this code:

console.log(wizard); // What could this be?
var wizard = "Gandalf";
Enter fullscreen mode Exit fullscreen mode

Manish's curious eyes widened. "Jagroop! You're trying to summon a wizard before declaring it!" he exclaimed. But to his surprise, the output wasn’t an error but undefined. 🀯

The Wise Old Interpreter πŸ§™β€β™‚οΈ

The JavaScript interpreter appeared out of nowhere and said, "Young coders, this phenomenon is called hoisting. In JavaScript, declarations of variables and functions are magically moved to the top of their scope during the compile phase."

He demonstrated:

// How JavaScript sees Jagroop's code:
var wizard;
console.log(wizard); // undefined
wizard = "Gandalf";
Enter fullscreen mode Exit fullscreen mode

The Function Spell ✨

Jagroop tried his luck with a function:

greet(); // "Hello, Codeville!"

function greet() {
  console.log("Hello, Codeville!");
}
Enter fullscreen mode Exit fullscreen mode

Manish clapped in awe. "Wow, it works even though you called it before declaring it!"

The interpreter explained, "Functions are also hoisted. But here’s the catch: entire function definitions are hoisted, not just their declarations. That's why your function works."

An Evil Trap 😈

But then Jagroop got overly confident and wrote this:

console.log(villain); // πŸ€·β€β™‚οΈ undefined
var villain = "Sauron";

console.log(sidekick); // 🚨 ReferenceError
let sidekick = "Samwise";
Enter fullscreen mode Exit fullscreen mode

The interpreter sighed and said, "Not all declarations behave the same. let and const are hoisted, but they don’t get initialized until the code execution reaches them. This is called the Temporal Dead Zone (TDZ)."

The Moral of the Story 🏰

Manish decided to summarize their learnings:

  1. var declarations are hoisted and initialized to undefined.
  2. Function declarations are fully hoisted (you can call them before declaring).
  3. let and const declarations are hoisted but stay in the TDZ until initialized.

The Wizard's Challenge πŸ§™β€β™‚οΈβ“

Here’s a puzzle for you, young coder! What will be the output of this code?

function test() {
  console.log(hero); // ?
  console.log(villain); // ?

  var hero = "Sauron";
  let villain = "Samwise";
}

test();
Enter fullscreen mode Exit fullscreen mode

Drop your answers in the comments! πŸ“βœ¨

Top comments (3)

Collapse
 
john12 profile image
john

Wow the concept of hoisting is explained in such a good story.This make easier to learn the concept.

Collapse
 
john12 profile image
john

It's output is

  • undefined
  • Reference Error
Collapse
 
jagroop2001 profile image
Jagroop Singh

Correct !!