0
0
Javascriptprogramming~10 mins

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

Choose your learning style9 modes available
Concept Flow - Function hoisting behavior
Start Execution
Parse Function Declarations
Hoist Functions to Top
Execute Code
Call Function
Run Function Body
End Execution
JavaScript moves function declarations to the top before running code, so functions can be called before they appear in the code.
Execution Sample
Javascript
console.log(greet());
function greet() {
  return "Hello!";
}
Calls a function before it is declared, showing function hoisting.
Execution Table
StepActionEvaluationResult
1Parse code and hoist function 'greet'Function 'greet' is stored in memoryFunction available before execution
2Execute console.log(greet())Call 'greet' functionFunction runs and returns 'Hello!'
3console.log outputsOutput stringHello!
4End of scriptNo more codeExecution stops
💡 All code executed, function hoisting allowed calling 'greet' before declaration
Variable Tracker
VariableStartAfter Step 1After Step 2Final
greetundefinedfunction greet() {...}function greet() {...}function greet() {...}
Key Moments - 2 Insights
Why can we call 'greet()' before its declaration?
Because function declarations are hoisted to the top during parsing (see execution_table step 1), so 'greet' exists before code runs.
What if 'greet' was a function expression assigned to a variable?
Then only the variable is hoisted as undefined, not the function. Calling it before assignment causes an error (not shown here).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'greet' after step 1?
Aundefined
Bnull
Cfunction greet() {...}
Dstring 'Hello!'
💡 Hint
Check the variable_tracker after Step 1 for 'greet'
At which step does the function 'greet' actually run?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at execution_table Action column where 'Call greet function' happens
If we replace the function declaration with a function expression assigned to a variable, what changes?
AFunction is still hoisted and callable before assignment
BVariable is hoisted but function is not, calling before assignment causes error
CNothing changes, code runs same way
DFunction runs twice automatically
💡 Hint
Refer to key_moments about function expressions vs declarations
Concept Snapshot
Function hoisting means JavaScript moves function declarations to the top before running code.
This allows calling functions before their code appears.
Only function declarations are hoisted fully, not function expressions.
Calling a function expression before assignment causes an error.
Use function declarations when you want hoisting behavior.
Full Transcript
In JavaScript, function declarations are hoisted to the top of their scope before the code runs. This means you can call a function before it appears in the code. For example, calling greet() before the function greet() is declared works because the function is stored in memory during parsing. The execution table shows parsing and hoisting first, then calling the function, then outputting the result. Variables holding function declarations have their function assigned at the start. However, if you use a function expression assigned to a variable, only the variable is hoisted as undefined, not the function itself. Calling it before assignment causes an error. This behavior helps understand why some functions can be called early and others cannot.