The Kahani Begins: Meet the Variables!

Ishant KumarIshant Kumar
3 min read

Imagine you’ve got three friends, each with a unique way of holding onto things. Let’s call them var, let, and const. They’re a little quirky, and each has their own rules.


Scene 1: var – The Forgetful Friend

Our friend var has been around for ages, practically a JavaScript grandparent. var used to be our go-to for storing data. You’d say:

var favoriteDrink = "Chai";

and just like that, JavaScript would remember it. But var has a few… quirks. For one, they’re forgetful about boundaries. Imagine var walking into a closed room, grabbing your belongings, and taking off with them. Not cool!

Let’s see what happens when var crosses boundaries:

if (true) {
  var snack = "Samosa";
}
console.log(snack); // Output: Samosa

Even though snack was declared inside a block, var lets it slip out into the rest of the code. This is called function scope, and it can get messy, especially as your code grows. var can even redeclare variables accidentally:

var favoriteDrink = "Tea";
var favoriteDrink = "Coffee"; // No errors here!
console.log(favoriteDrink); // Output: Coffee

This leads to confusion, which is why we don’t use var as much anymore.


Scene 2: let – The Reliable Friend

Now we meet let, the more reliable friend. If you tell let something, they respect the boundaries of where you declared it.

let favoriteDrink = "Chai";
if (true) {
  let snack = "Samosa";
  console.log(snack); // Output: Samosa
}
console.log(snack); // Error! snack is not defined outside the block

See that? Unlike var, let keeps snack within the block it was declared in. No sneaking around outside the block! And let won’t let you redeclare a variable by mistake.

let favoriteDrink = "Chai";
let favoriteDrink = "Coffee"; // This would cause an error!

You can, however, change the value of a let variable:

let chaiCups = 3;
chaiCups = 4; // Works fine
console.log(chaiCups); // Output: 4

Scene 3: const – The Unchangeable Friend

Finally, there’s const, the friend who is a bit of a rock. Once you tell const something, they’ll remember it exactly as is – no changes allowed.

const birthYear = 1995;
birthYear = 1996; // Error! You can't change a constant

With const, you’re saying, “This value is final. Do not change it!” Perfect for things that shouldn’t change, like a user’s birth year or the value of pi. However, if you’re dealing with an object or array, there’s a little flexibility – you can modify properties inside it but can’t reassign the whole thing.

const favoriteSnacks = ["Chai", "Samosa"];
favoriteSnacks.push("Pakora");
console.log(favoriteSnacks); // Output: ["Chai", "Samosa", "Pakora"]

But try to reassign it:

favoriteSnacks = ["Biscuits"]; // Error! You can’t reassign the array itself

Final Thought: When to Use let and const?

In today’s JavaScript, the best practice is to use const for values you know won’t change and let for values that might change later. And var? Well, maybe leave var in the past with floppy disks and dial-up internet! Here’s a simple example to follow:

const birthYear = 1990; // Fixed value
let currentAge = 30; // This may change
currentAge += 1; // Now currentAge is 31

And That’s Today’s Kahani!

With var, let, and const, you’re ready to handle data like a pro in JavaScript. Each one has its own story, and choosing the right one makes your code cleaner, safer, and easier to understand. So next time you declare a variable, remember to pick the right friend: reliable let or steadfast const. And if you still have love for var, no judgment – just know their quirks before diving in!

Join us next time on JavaScript aur Kahani for more stories from the world of code!

0
Subscribe to my newsletter

Read articles from Ishant Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ishant Kumar
Ishant Kumar

Coffee into code, and bugs into pull requests. Always up for debating if TypeScript really makes JavaScript a better person.