1.4 — let vs const vs var
You’ve been creating variables with let. But some values should never change: the value of pi, a tax rate, the address of the server your tests talk to. For those, JavaScript gives you a second keyword — const — which welds the label on. And lurking in older tutorials there’s a third, var, that you need to recognize but shouldn’t write. Three keywords, one clear rule by the end.
let score = 10; score = 20; const pi = 3.14; pi = 3.15;
The top pair is yesterday’s news: a let box swaps its contents without complaint. let means “this variable is allowed to change.”
The deeper story, with the real names for things — this part is what turns “I saw it” into “I can explain it.”
Why does const exist at all, when let can do everything? Because code is read far more often than it is written. A const tells every future reader: “stop tracking this one; it never changes.” Future you is one of those readers. So are the teammates whose test suites you will maintain. In a 500-line file, knowing 40 of 50 variables are frozen is a gift to your memory. const is less about the machine and more about communication between humans.
One honest note, because this app never lies to you. For every value you know so far — numbers, strings, booleans — const truly freezes things. What you learned today is the whole story. In Phase 4 you will meet bigger kinds of values (arrays, objects) where const carries one surprising piece of fine print. We open it exactly when you can watch it happen — lesson 4.6. A preview, not homework.
And var, concretely. First a word: scope means “which part of the code can see this variable”. let and const keep a variable inside the block where it was born — like the inside of an if. var ignores that boundary and leaks out. Programmers say var is “function-scoped”, not “block-scoped” (fully clicks in 3.5).
It also behaves oddly when used before its line: no error, you silently get undefined (the full mechanism is lesson 5.2). Until both fully click, the practical rule stands: read var fluently, write let and const only.
⌨️ the right keyword for the job
Two facts to store: one will never change, one changes all the time. Picking the right keyword for each — that IS the exercise.
requirements:
- A variable named
nameholds"Lijas". A person’s name doesn’t change. - A variable named
moodstarts as"sleepy"— and later becomes"curious"(the coffee kicked in). - Print
name, then the finalmood— in that order.
when you press RUN, the console must show exactly:
⌨️ the visitor counter
A counter that counts visitors as they arrive. It has to survive being changed — twice.
requirements:
- A variable named
countstarts at the number0— a number, so no quotes. - First visitor:
countbecomes1— print it. Second visitor: it becomes2— print it again. - Same variable the whole time. Choose its keyword so the changes don’t crash.
when you press RUN, the console must show exactly:
✏️ Quick check 1
The second line halts the program. Type the NAME of the error (one word, ends in Error).
const city = "Chennai"; city = "Mumbai";
✏️ Quick check 2
You’re creating a variable and you’re not sure yet if it will need to change. Type the keyword professionals reach for FIRST.
✏️ Quick check 3
You open a tutorial from 2012 and every line uses var. What’s the right move?
🗣️ Now teach it back
Explain to a friend: what’s the difference between let and const, why do professionals default to const, and what should they do when they meet var in an old tutorial?
Write it as if your friend is sitting next to you. Saved to your journal — future-you will use these notes to teach others.