var, let, const & hoisting
Understand the idea
A counter works until its second click, then throws “Assignment to constant .” The declaration promised a that would never change.
A name tag for a value
Changing the tag or its contents
Use const when you will not reassign that binding. Use let when reassignment is part of the job, such as updating a running count. const needs an initializer. It does not make an object or : their can still change through the same binding.
A binding is simply the connection between a name and its current value. Start with const when writing a declaration, then choose let if the code really needs to move that name to a different value. This lets the next reader see your intention without hunting through every later line.
| Declaration | Can reassign? | Scope | Read before declaration? |
|---|---|---|---|
| const | No; object contents may still change | Enclosing block | ReferenceError until initialized |
| let | Yes | Enclosing block | ReferenceError until initialized |
| var | Yes | Enclosing function or script global scope | undefined before its assignment |
Keeping a name local
Both let and const belong to their enclosing . var belongs to the enclosing , or a wider script scope when there is no function. That difference makes var inside an if visible after the if. Prefer the narrower declaration when writing new code so a local change stays local.
What hoisting actually changes
Hoisting describes declaration behavior before the declaration's line executes. A var binding is initialized to undefined before its assignment runs. A let or const binding exists in its scope but cannot be read before initialization. That interval is the . It throws instead of giving you an early value.
JavaScript does not physically move the lines you wrote. It prepares bindings before executing their scope. An assignment still happens where you wrote it. This is why seeing undefined before a var assignment does not mean the intended value has already been calculated.
The loop surprise
Function declarations can usually be called earlier in their scope, while a function assigned to const must wait for initialization. Put declarations before uses when you can. In a loop, the more subtle question is whether later work keeps reading one shared binding or a different binding for each pass.
Here is the loop consequence in words: schedule three later in a loop counting 0, 1, and 2. With var, all three callbacks read the same binding after it reaches 3, so all print 3. With let in the loop header, each gets its own binding, so the callbacks read 0, 1, and 2. It is the binding that differs, not the speed of the timer. The lesson returns to why a callback can still read it later.
Mental model
FOLLOW THE VALUE
const keeps this binding attached to the same object.
01 / 03Trace the transformation
Live example
The is even though settings is const. Only visits is reassigned, so only visits needs let.
Your console output appears here.
Guided challenge
Allow the counter to change
Keep settings as const. Change only the declaration that must allow reassignment.
Your console output appears here.
EXPECTED CONSOLE OUTPUT
dark 2
Hints 0/2
Use all hints or make 3 failed runs (0/3).
Free sandbox
Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.
Your console output appears here.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 100 XP. You can always revisit it.