0
0
Javascriptprogramming~10 mins

Variable hoisting behavior in Javascript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Variable hoisting behavior
Start Execution
Parse code
Hoist declarations
Initialize variables
Execute code line by line
Use variables
End Execution
JavaScript first moves variable declarations to the top (hoisting), then runs code line by line, so variables exist before their declaration line but may be undefined.
Execution Sample
Javascript
console.log(a);
var a = 5;
console.log(a);
This code logs the value of 'a' before and after it is assigned, showing hoisting behavior.
Execution Table
StepCode LineActionVariable 'a' ValueOutput
1Hoisting phaseDeclaration of 'a' hoisted, initialized as undefinedundefined
2console.log(a);Logs current value of 'a'undefinedundefined
3var a = 5;Assigns 5 to 'a'5
4console.log(a);Logs current value of 'a'55
5EndExecution finished5
💡 Reached end of code, all lines executed
Variable Tracker
VariableStart (before execution)After Step 1After Step 3Final
anot declaredundefined55
Key Moments - 3 Insights
Why does the first console.log print 'undefined' instead of causing an error?
Because the declaration of 'a' is hoisted and initialized as undefined before code runs, so 'a' exists but has no value yet (see execution_table step 2).
Does the assignment 'a = 5' get hoisted too?
No, only the declaration is hoisted. The assignment happens in order during execution (see execution_table step 3).
What would happen if we used 'let' instead of 'var' for 'a'?
Using 'let' does not hoist the variable in the same way; accessing it before declaration causes a ReferenceError.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'a' at Step 2?
A5
Bundefined
CReferenceError
Dnull
💡 Hint
Check the 'Variable 'a' Value' column at Step 2 in the execution_table.
At which step does 'a' get assigned the value 5?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Action' column describing assignment in the execution_table.
If we replace 'var a = 5;' with 'let a = 5;', what would happen at Step 2?
AIt logs 'undefined'
BIt logs 5
CIt throws a ReferenceError
DIt logs null
💡 Hint
Recall that 'let' variables are not hoisted like 'var' and accessing before declaration causes error.
Concept Snapshot
JavaScript hoists variable declarations (var) to the top.
Variables exist before their declaration line but are initialized as undefined.
Assignments are not hoisted, they happen in order.
Accessing var before assignment logs undefined, not error.
Using let/const changes hoisting behavior and causes errors if accessed early.
Full Transcript
In JavaScript, variable declarations using 'var' are hoisted to the top of their scope before code runs. This means the variable exists from the start but is set to undefined until the assignment line executes. For example, logging a variable before its declaration prints undefined, not an error. The assignment happens in order during execution. Using 'let' or 'const' changes this behavior: accessing them before declaration causes a ReferenceError. This trace shows step-by-step how 'var a' is hoisted, initialized as undefined, then assigned 5, and how console.log outputs reflect these states.