Variables in JavaScript: const, let, and var
Programs become useful when they can remember information:
- The username of a logged-in student
- The items in a shopping cart
- The score in a browser game
- The price of a plane ticket
In JavaScript, we use variables to store and label data values in memory.
By the end of this lesson, you will understand:
- How to declare variables using modern JavaScript (
constandlet) - Why
varis legacy and dangerous in modern web applications - What Block Scope
{ ... }means - When to use
constvs.let
The 3 Ways to Declare a Variable
JavaScript gives us three keywords to create variables:
const birthYear = 2004; // 🔒 Cannot be changed (Constant)let currentAge = 20; // 🔓 Can change over timevar legacyName = "Aarav"; // ⚠️ Outdated 1995 syntax (Avoid!)Let's examine how each one behaves.
1. const (Constants): Your Safe Default 🔒
In modern JavaScript development, const should be your default choice.
When you declare a variable with const, you create an immutable binding—meaning the variable cannot be reassigned to a different value:
const pi = 3.14159;const country = "India";// Attempting to change it:pi = 3; // 💥 TypeError: Assignment to constant variable!Why is const so popular among senior developers?
In large software applications with hundreds of files, bugs often happen when one part of the code accidentally overwrites a variable that another part depends on. Using const guarantees that once a variable is created, its assignment will never be corrupted.
2. let: When Values Must Change 🔓
What if a value needs to change as your program runs?
- A running score in a game (
score = score + 10) - A loop counter (
i = i + 1) - A toggle switch (
isLoggedIn = true)
For values that change, use let:
let score = 0;console.log(score); // Prints: 0score = 15; // ✅ Perfectly allowed!console.log(score); // Prints: 15Notice that we used let only when creating the variable. When reassigning a new value later, you simply use the variable's name without repeating let.
3. var: The Ghost of 1995 ⚠️
Before modern JavaScript (ES6) was introduced in 2015, var was the only way to declare variables.
Today, professional JavaScript developers never use var. Here is why:
The Scope Leak Problem:
In JavaScript, curly braces { ... } define a Block (like inside an if statement or a loop).
Variables declared with let and const are block-scoped—they stay safely trapped inside their curly braces.
var, however, completely ignores curly braces and leaks out into the surrounding function or global window!
if (true) { var leakedVar = "I escaped!"; let safeLet = "I am trapped!";}console.log(leakedVar); // Prints: "I escaped!" (Scope leak!)console.log(safeLet); // 💥 ReferenceError: safeLet is not definedImagine if you rented a hotel room, and the hotel let guests in other rooms walk through your walls. That is what var does to your code!
Interactive Variables & Scope Simulator 🕹️
Experiment with const, let, and var in the live scope chamber below. See how block boundaries trap safe variables, how reassignment behaves, and why var leaks:
const taxRate = 0.18; taxRate = 0.20; // 💥 Attempting reassignment
Modern JavaScript Golden Rule: Always use const by default. If you know a variable's value must change (like a loop counter), use let. Never use var in modern code!
Summary Cheatsheet: Which Keyword Should You Use?
| Feature | const | let | var (Legacy) |
|---|---|---|---|
| Can be reassigned? | ❌ No | ✅ Yes | ✅ Yes |
| Can be redeclared? | ❌ No | ❌ No | ⚠️ Yes (Dangerous!) |
| Scope boundary | 🛡️ Block-scoped { } | 🛡️ Block-scoped { } | ❌ Function-scoped (Leaks from { }) |
| Hoisting behavior | 🛑 Temporal Dead Zone (TDZ) | 🛑 Temporal Dead Zone (TDZ) | ⚠️ Hoisted as undefined |
| When to use? | Always by default (90% of code) | Only when value must change | Never in modern code |
[!TIP] The Golden Rule of JavaScript Variables Always use
constfirst. If you discover that the variable's value needs to be reassigned later, change it tolet. Never usevar!