let and const
Storing information with the right keyword — and why we skip var.
Step 1
The idea
Imagine two kinds of notepads:
A whiteboard — you write something, then erase it and write something else. The value can change.
Permanent marker on a wall — once written, it stays. You can read it any time, but you can't change it.
In JavaScript, let is the whiteboard and const is the permanent marker. Both store information. The difference is whether the value can change later.
We skip var entirely — it's the old way from 1995 and has confusing behaviour. Nobody writes new code with it.
Step 2
See it run
Priya
Hyderabad
Beginnerconst name = "Priya"; // won't change — use const
let city = "Hyderabad"; // might change — use let
let level = "Beginner"; // might change as they learn
console.log(name);
console.log(city);
console.log(level);
// Later in the program, city can change:
city = "Mumbai";
console.log(city); // Mumbai$ node variables.jsStep 3
Now you try
Copy the file above. Then try to change the const value:
const name = "Priya";
name = "Rahul"; // ← try adding this lineRun it. You'll see:
TypeError: Assignment to constant variable.That error is JavaScript protecting you — you declared something as permanent and then tried to change it. Now delete that line and run again. No error.
Step 4
Why a tester cares
In any real program, you use const for things that are fixed and let for things that update. Here's a retry counter — a common pattern in automation scripts:
const MAX_RETRIES = 3; // const — this limit never changes
let attempts = 0; // let — this increases each time
let success = false; // let — flips to true when done
while (attempts < MAX_RETRIES && !success) {
attempts++;
console.log(`Attempt ${attempts}...`);
if (attempts === 2) {
success = true; // simulating a successful attempt
}
}
console.log(`Finished after ${attempts} attempt(s). Success: ${success}`);$ node retry.jsNotice how MAX_RETRIES is a great use of const — you set it once and it never changes. attempts and success are let because they change as the program runs.
Recap
- 1const = permanent marker. Declare once, never reassign. Use it for most things.
- 2let = whiteboard. Can be reassigned later. Use it when the value needs to change.
- 3Never use var — it has confusing scoping rules that cause real bugs.
Up next
Lesson 1.4 — Data types