Bragadeesh’s Substack

Bragadeesh’s Substack

Navigating Variable Declarations in JavaScript: A Tale of let, var, and const

Bragadeesh's avatar
Bragadeesh
Dec 29, 2023
∙ Paid
Share

In the bustling city of JavaScript, where lines of code construct the digital world we interact with, variables stand as the foundational building blocks, holding, and managing data throughout our applications. Today, let’s embark on a journey through the streets of JavaScript, exploring the distinct neighborhoods of let, var, and const, understanding their unique characteristics, and discerning when and how to visit (use) each one.

Photo by Caspar Camille Rubin on Unsplash

🚗 var: The Classic, Hoisting Vehicle of JavaScript

In the early days of JavaScript city, var was the only means to declare variables. But var comes with its quirks, notably hoisting, where variables are lifted to the top of their functional or global scope, regardless of where they are declared.

Example:

console.log(x); // undefined, not ReferenceError!
var x = 5;

Imagine declaring to pay at a shop (variable declaration) and being allowed to take the goods (use the variable) even before you actually declare to pay. That’s var for you - a bit lenient and potentially confusing for the uninitiated traveler.

Keep reading with a 7-day free trial

Subscribe to Bragadeesh’s Substack to keep reading this post and get 7 days of free access to the full post archives.

Already a paid subscriber? Sign in
© 2025 Bragadeesh
Privacy ∙ Terms ∙ Collection notice
Start writingGet the app
Substack is the home for great culture