Note: Read this only if you're either learning JavaScript right now or already know a little about it. If you haven't touched JS yet, bookmark this for later — it'll make a lot more sense once you have.

I wrote var for the first two months of my JavaScript journey. Every tutorial used it. Every YouTube video used it. Nobody told me it was basically outdated and could silently break my code in ways I'd spend hours debugging. If you're still defaulting to var without knowing why — this article is for you.

The frustrating part is that var works. Your code won't crash. Your browser won't yell at you. The bugs it creates are silent — the kind that show up at the worst possible moment and take forever to trace back to the source. That's exactly what makes it dangerous for beginners.

What is var — And Why It's Problematic

Back in 1995, var was the original, old-school way to declare variables in JavaScript. While it worked fine for simple scripts in the early days of the web, it has a massive architectural defect: var is function-scoped rather than block-scoped.

To put that in plain English, "block scope" just means that whatever you create inside curly braces {} — like an if statement or a loop — is supposed to stay locked inside those braces. But var completely ignores this rule; it casually leaks right out of your loops and if statements, accidentally messing with the rest of your code.

Note: No worries if you don't know loops or if statements yet — just know that whatever is supposed to stay inside curly braces in JS, doesn't stay inside when var is used.

To make things even weirder, var does something called "hoisting." JavaScript quietly lifts the variable declaration to the very top of your file behind the scenes. This means you can actually use a var variable before you even create it in your code — and instead of crashing with a helpful error message, your code keeps running and just gives you a confusing undefined.

Silent bugs are the worst kind because your code doesn't crash to warn you — it just silently breaks your website. Because var is full of these hidden traps, using it today is just asking for a headache.

What is let — And When to Use It

To save everyone from the problems of var, JavaScript introduced let in 2015 as part of the ES6 update. It was created specifically to fix those old, broken behaviors and bring sanity back to coding.

The biggest strength of let is that it's strictly block-scoped. If you create a let variable inside curly braces — like a loop or an if statement — it stays locked exactly where you put it and won't leak out to mess with other parts of your file.

It also fixes the weird hoisting issue. If you try to use a let variable before declaring it, JavaScript won't give you a silent undefined — instead, it immediately throws a proper error and stops running. Proper error messages are your best friend because they point out mistakes instantly instead of letting them hide.

Use let whenever you know the value of a variable is going to change down the line — things like a counter in a loop, a player's score in a game, or a toggle state that flips between true and false.

What is const — And Why It Should Be Your Default

Introduced alongside let in the ES6 update, const is short for "constant." Just like let, const is strictly block-scoped — it stays locked safely inside the curly braces where you created it.

The big difference — once you assign a value to a const variable, you cannot reassign it later in your code. This creates a common misunderstanding for beginners: it does not mean the underlying value is completely frozen. If you store an object or an array inside a const, you can still modify the data inside it — like adding an item to a list or changing a property. What you cannot do is overwrite the variable by pointing it at a completely new value. Assigning a new value and modifying an existing one are two completely different things.

Make const your default choice whenever you create a new variable. Only switch to let when you're certain the value needs to change later. This one habit alone makes your code cleaner, more predictable, and far easier to debug.

The Simple Rule You Need to Remember

You don't need to overcomplicate this. Lock this three-step rule into your brain and you'll be fine:

This isn't a random opinion — it's the modern JavaScript standard every professional developer follows today. Most modern code editors and team tools will actually throw a warning if you type var into your file.

A Quick Visual Comparison

Let's look at exactly how these three behave in real code, so you can see the difference with your own eyes.

var vs let vs const — Side by Side
// 1. The 'var' behavior (The Leak)
if (true) {
  var greeting = "Hey there!";
}
console.log(greeting); // "Hey there!" — it leaked out!

// 2. The 'let' behavior (The Safe Lock)
if (true) {
  let secret = "Keep it safe";
}
console.log(secret); // Error: secret is not defined — locked inside!

// 3. The 'const' behavior (The Constant Guard)
const score = 100;
score = 200; // Error: Assignment to constant variable

Seeing it side-by-side makes it click instantly. var escapes the block and runs loose, let stays locked safely inside its braces, and const stays locked while firmly protecting its value from being changed.

Open any JavaScript file you've written recently. Search for the word var. Replace every single one with either const or let — const if the value doesn't change, let if it does. Run your code. If something breaks, you just found a real bug that var was hiding from you. Fix it. That's how you actually learn this.