JavaScript Roadmap — Day 3: Variables — var, let, and const
Variables — var, let, and const
Variables are containers that store data. Choosing the right one — var, let, or const — is the foundation of writing clean, bug-free JavaScript.
Look at this code. A developer wrote it in 2015 — and it has a hidden bug that took 3 hours to find:
The problem:
var does not respect block boundaries — it leaks out of if blocks, for loops, and any other {}. This causes variables to exist in places they should not — leading to subtle, hard-to-find bugs. The fix is simple: use let instead. This is exactly why let and const were added to JavaScript in 2015.
Every program needs to store data — names, numbers, scores, prices. In JavaScript we use variables to store this data. There are three ways to create them. Choosing the right one is not just style — it is the difference between code that works correctly and code that has hidden bugs.
| Feature | var | let | const |
|---|---|---|---|
| Scope | Function scope | Block scope | Block scope |
| Reassign | ✅ Yes | ✅ Yes | ❌ No |
| Redeclare | ✅ Yes | ❌ No | ❌ No |
| Hoisting | Yes (undefined) | Yes (TDZ) | Yes (TDZ) |
| Use today? | ❌ Avoid | ✅ Yes | ✅ Prefer |
1. What is a Variable?
A variable is a named container that holds a value. Think of it like a labeled box — you put data inside, give it a name, and use that name to find the data later. Without variables you would have to type the same values over and over everywhere in your code.
💡 Naming rules: Variable names must start with a letter, $, or _. They cannot start with a number. Use camelCase — myVariableName not my_variable_name. Names are case-sensitive — score and Score are two completely different variables.
2. let — The Modern Variable
let was introduced in ES6 (2015) to fix the problems with var. Use let when the value will change — like a counter, a score, a user's current selection, or anything that gets updated during the program.
3. const — The Constant
const declares a variable that cannot be reassigned. Use it for values that should never change — API keys, configuration, mathematical constants, or any value you want to protect from accidental change. In modern JavaScript const is the default choice — use it unless you know the value will change.
🔥 The most important thing about const: const prevents you from reassigning the variable — not from changing the contents of an object or array. This surprises almost every beginner. const locks the box — but you can still change what is inside the box. Only the label is permanent.
4. var — Why You Should Avoid It
var was the only way to declare variables before ES6. It works — but it has three behaviors that cause real bugs. Every modern JavaScript codebase has moved away from var. You need to understand it because you will see it in old code — but never use it in new code.
5. Scope Deep Dive — Where Variables Live 🔥
Scope is the region of your code where a variable exists and can be accessed. Understanding scope is one of the most important concepts in JavaScript — it explains why variables sometimes seem to disappear or cause unexpected errors.
const. Only change to let if you get an error saying you cannot reassign. This habit makes your code safer, more readable, and tells other developers that this value is intentionally fixed. Never start with var.userAge is infinitely better than x. isLoggedIn is better than flag. totalCartPrice is better than total. You will read your code far more than you write it — name things for the reader, not the writer.const prevents reassignment of the variable — but object properties and array elements can still change. If you truly need an immutable object use Object.freeze(obj). This is an advanced pattern but good to know exists for when you need it.6. Hoisting — The Hidden Behavior
Hoisting is JavaScript's behavior of moving variable and function declarations to the top of their scope before code runs. It sounds confusing but understanding it explains some very common bugs — especially with var.
7. Try It Yourself
Edit the code and click Run Code. Try uncommenting the error lines to see what happens. Experiment with scope by moving variables inside and outside blocks.
8. Practice Tasks
Task 1 — Easy: Declare All Three
Declare one variable with var, one with let, one with const. Try reassigning each one. Observe which one throws an error. Write a comment next to each explaining when you would use it in real code.
Task 2 — Medium: Block Scope Test
Create a block using {}. Inside it declare a variable with let and another with var. Try accessing both outside the block. Notice that let throws a ReferenceError but var does not — write a comment explaining why this is actually a feature, not a bug.
Task 3 — Hard: Your Profile with const and Object.freeze
Create a const profile object with your name, age, city, and isStudent. First try modifying a property — it should work. Then wrap it with Object.freeze(profile) and try modifying again — what happens now? Log the profile before and after. This is how you create truly immutable data in JavaScript.