DEV Community

Cover image for The Tale of Three Variables: let, const, and var in JavaScript Land
Chandan Singh
Chandan Singh

Posted on

The Tale of Three Variables: let, const, and var in JavaScript Land

Once upon a time in the magical realm of JavaScript Land, there were three remarkable characters: var, the old guardian; let, the flexible friend; and const, the steadfast sentinel. Together, they held the secrets of variable declaration, each with its unique characteristics. Journey with us as we explore their stories and unveil their mysteries.

Chapter 1: var, the Old Guardian

A Wise But Confusing Character
In the early days of JavaScript Land, var was the most experienced guardian. He had a knack for adapting to various situations, but his function-scoped nature sometimes led to confusion.

function confusion() {
 var a = 10;
 if (true) {
 var a = 20;
 }
 console.log(a); // Outputs 20
}
Enter fullscreen mode Exit fullscreen mode

In the tale of the young programmer Emily, var helped her define variables, but his hoisting behaviour perplexed her.

“Why does it behave like that?” Emily pondered. var smiled and said, “I’m function-scoped, dear Emily. I have my own rules.”

Chapter 2: let, the Flexible Friend

A Modern and Adaptable Resident
As the era of ES6 unfolded in JavaScript Land, let made its appearance. More predictable and contemporary than var, it brought a friendly and modern touch to the realm.

function clarity() {
 let b = 10;
 if (true) {
 let b = 20;
 }
 console.log(b); // Outputs 10
}
Enter fullscreen mode Exit fullscreen mode

One day, let met a developer named Jack, struggling with a problem that var couldn’t solve. let introduced himself, “I’m block-scoped, Jack. I behave differently within different blocks of code.” Jack’s code worked flawlessly, and they became fast friends.

Chapter 3: const, the Steadfast Sentinel

A Reliable and Unchangeable Character
In the same era as let, const stood tall as a sentinel, guarding values with unwavering dedication.

const c = 10;
// c = 20; // This will throw an error
Enter fullscreen mode Exit fullscreen mode

In a tale where a critical piece of information had to be preserved, const was the hero of the day. "I'm immutable," he declared, "Once assigned, my value shall never change!" The people of JavaScript Land praised his reliability.

The Gathering of the Three

One beautiful day, the three characters gathered to share their stories. Here’s a summary of their characteristics:

comparison between let, const and var

They laughed and celebrated their unique strengths, knowing that each had a place in the heart of JavaScript Land.

Conclusion: Lessons from JavaScript Land

And so, dear reader, we’ve explored the fascinating tales of let, const, and var. Each has its role and purpose:

Use var if you need function scope (though its usage is less common these days).
Choose let when you need a variable that can be reassigned within block scope.
Opt for const when you want to preserve a value that should never change.

Embark on your own adventures in JavaScript Land, and may these three characters guide you along the way. Happy coding!

Top comments (0)