Skip to content
engineering workbench.by StudyPort Academy
Learning path
Foundations15 minLESSON 02 / 34

var, let, const & hoisting

01

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.

The three declaration rules side by side
DeclarationCan reassign?ScopeRead before declaration?
constNo; object contents may still changeEnclosing blockReferenceError until initialized
letYesEnclosing blockReferenceError until initialized
varYesEnclosing function or script global scopeundefined 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.

02

Mental model

FOLLOW THE VALUE

settings
name tag
one object

const keeps this binding attached to the same object.

01 / 03Trace the transformation

03

Live example

The is even though settings is const. Only visits is reassigned, so only visits needs let.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022
04

Guided challenge

Allow the counter to change

Keep settings as const. Change only the declaration that must allow reassignment.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022

EXPECTED CONSOLE OUTPUT

dark
2

Hints 0/2

    Use all hints or make 3 failed runs (0/3).

    05

    Free sandbox

    Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.

    Opening editor…
    LIVE PREVIEW
    Console 0

    Your console output appears here.

    Saved on this deviceJavaScript · ES2022
    06

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01Which change is allowed for a const object?
    02What happens when let is read before its declaration?

    Ready to call this one understood?

    Mark this lesson complete to earn 100 XP. You can always revisit it.

    Sign in to save progress
    Next lesson