Bird
Raised Fist0
Javascriptprogramming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which statement best describes function hoisting in JavaScript?
easy
A. Function declarations are hoisted and can be called before they are defined.
B. Function expressions are hoisted and can be called before assignment.
C. Variables declared with let are hoisted like functions.
D. Arrow functions are hoisted like function declarations.

Solution

  1. Step 1: Understand hoisting for function declarations

    Function declarations are moved to the top of their scope, so they can be called before their definition in code.
  2. Step 2: Compare with function expressions and other types

    Function expressions and arrow functions are not hoisted in the same way; they cannot be called before assignment.
  3. Final Answer:

    Function declarations are hoisted and can be called before they are defined. -> Option A
  4. Quick Check:

    Function declarations hoisted = C [OK]
Hint: Only function declarations are hoisted fully [OK]
Common Mistakes:
  • Thinking function expressions are hoisted like declarations
  • Confusing variable hoisting with function hoisting
  • Assuming arrow functions are hoisted
2. Which of the following is the correct syntax to declare a function that will be hoisted?
easy
A. function myFunc() { return 1; }
B. var myFunc = () => 1;
C. let myFunc = () => 1;
D. const myFunc = function() { return 1; };

Solution

  1. Step 1: Identify function declaration syntax

    Function declarations use the function keyword followed by the function name and body.
  2. Step 2: Check other options for hoisting behavior

    Options A, B, and C are function expressions or arrow functions assigned to variables, which are not hoisted as functions.
  3. Final Answer:

    function myFunc() { return 1; } -> Option A
  4. Quick Check:

    Function declaration syntax = D [OK]
Hint: Only function declarations use 'function name()' syntax [OK]
Common Mistakes:
  • Confusing function expressions with declarations
  • Assuming arrow functions are hoisted
  • Using variable assignment syntax for hoisted functions
3. What will be the output of the following code?
console.log(foo());

function foo() {
  return 'Hello';
}

console.log(bar());

var bar = function() {
  return 'World';
};
medium
A. SyntaxError
B. ReferenceError for bar
C. 'Hello' followed by 'World'
D. 'Hello' followed by TypeError

Solution

  1. Step 1: Analyze the call to foo()

    The function foo is a declaration and hoisted, so foo() returns 'Hello'.
  2. Step 2: Analyze the call to bar()

    bar is a variable assigned a function expression. The variable is hoisted but initialized as undefined at the time of the call, so calling bar() causes a TypeError.
  3. Final Answer:

    'Hello' followed by TypeError -> Option D
  4. Quick Check:

    Function declaration hoisted, function expression not initialized = A [OK]
Hint: Function declarations hoisted, expressions not initialized [OK]
Common Mistakes:
  • Expecting both functions to run without error
  • Confusing ReferenceError with TypeError
  • Assuming variable functions are hoisted fully
4. Consider the code below. It throws an error. What is the fix?
console.log(add(2, 3));

const add = function(a, b) {
  return a + b;
};
medium
A. Move the console.log after the function expression.
B. Use var add instead of const add.
C. Change const add to function add declaration.
D. Wrap the function expression in parentheses.

Solution

  1. Step 1: Identify why error occurs

    The variable add is declared with const and assigned a function expression. It is not hoisted as a function, so calling it before assignment causes a ReferenceError.
  2. Step 2: Fix by using function declaration

    Changing to a function declaration function add(a, b) hoists the function, allowing calls before its definition.
  3. Final Answer:

    Change const add to function add declaration. -> Option C
  4. Quick Check:

    Function declarations hoisted, expressions not [OK]
Hint: Use function declarations to call before definition [OK]
Common Mistakes:
  • Assuming var fixes hoisting for function expressions
  • Thinking moving console.log is the only fix
  • Trying to wrap function expression in parentheses
5. You want to organize your code so that helper functions can be called anywhere in the file without errors. Which approach best uses function hoisting to achieve this?
hard
A. Assign all helper functions to const variables at the top of the file.
B. Use function declarations anywhere; they are hoisted to the top automatically.
C. Declare all helper functions using function declarations at the bottom of the file.
D. Wrap helper functions in arrow functions assigned to let variables.

Solution

  1. Step 1: Understand hoisting behavior for function declarations

    Function declarations are hoisted to the top of their scope, so they can be called anywhere in the file regardless of their position.
  2. Step 2: Compare with other approaches

    Assigning functions to variables (const or let) does not hoist the function body, so calls before assignment cause errors.
  3. Final Answer:

    Use function declarations anywhere; they are hoisted to the top automatically. -> Option B
  4. Quick Check:

    Function declarations hoisted anywhere = A [OK]
Hint: Function declarations can be called anywhere due to hoisting [OK]
Common Mistakes:
  • Thinking function expressions are hoisted
  • Placing function declarations only at bottom
  • Using arrow functions expecting hoisting
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard