Function hoisting behavior in Javascript - Time & Space Complexity
We want to understand how the order of function declarations affects the time it takes for JavaScript to run code.
Specifically, does calling a function before it is written change how long the program takes to run?
Analyze the time complexity of the following code snippet.
function greet() {
return "Hello!";
}
console.log(greet());
const sayBye = function() {
return "Goodbye!";
};
console.log(sayBye());
This code calls two functions: one declared normally and one assigned to a variable.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: There are no loops or repeated operations here; each function is called once.
- How many times: Each function runs exactly one time.
Since there are no loops or repeated calls, the time to run this code does not grow with input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 2 function calls |
| 100 | 2 function calls |
| 1000 | 2 function calls |
Pattern observation: The number of operations stays the same no matter how big the input is.
Time Complexity: O(1)
This means the time to run the code stays constant and does not increase with input size.
[X] Wrong: "Calling a function before it is declared always slows down the program."
[OK] Correct: JavaScript moves function declarations to the top before running code, so calling them early does not add extra time.
Understanding how JavaScript handles functions helps you write clearer code and explain behavior confidently in interviews.
"What if we replaced the function expression with an arrow function assigned to a variable? How would the time complexity change?"
Practice
Solution
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.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.Final Answer:
Function declarations are hoisted and can be called before they are defined. -> Option AQuick Check:
Function declarations hoisted = C [OK]
- Thinking function expressions are hoisted like declarations
- Confusing variable hoisting with function hoisting
- Assuming arrow functions are hoisted
Solution
Step 1: Identify function declaration syntax
Function declarations use thefunctionkeyword followed by the function name and body.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.Final Answer:
function myFunc() { return 1; } -> Option AQuick Check:
Function declaration syntax = D [OK]
- Confusing function expressions with declarations
- Assuming arrow functions are hoisted
- Using variable assignment syntax for hoisted functions
console.log(foo());
function foo() {
return 'Hello';
}
console.log(bar());
var bar = function() {
return 'World';
};Solution
Step 1: Analyze the call to
The functionfoo()foois a declaration and hoisted, sofoo()returns 'Hello'.Step 2: Analyze the call to
bar()baris a variable assigned a function expression. The variable is hoisted but initialized asundefinedat the time of the call, so callingbar()causes a TypeError.Final Answer:
'Hello' followed by TypeError -> Option DQuick Check:
Function declaration hoisted, function expression not initialized = A [OK]
- Expecting both functions to run without error
- Confusing ReferenceError with TypeError
- Assuming variable functions are hoisted fully
console.log(add(2, 3));
const add = function(a, b) {
return a + b;
};Solution
Step 1: Identify why error occurs
The variableaddis declared withconstand assigned a function expression. It is not hoisted as a function, so calling it before assignment causes a ReferenceError.Step 2: Fix by using function declaration
Changing to a function declarationfunction add(a, b)hoists the function, allowing calls before its definition.Final Answer:
Changeconst addtofunction adddeclaration. -> Option CQuick Check:
Function declarations hoisted, expressions not [OK]
- Assuming var fixes hoisting for function expressions
- Thinking moving console.log is the only fix
- Trying to wrap function expression in parentheses
Solution
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.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.Final Answer:
Use function declarations anywhere; they are hoisted to the top automatically. -> Option BQuick Check:
Function declarations hoisted anywhere = A [OK]
- Thinking function expressions are hoisted
- Placing function declarations only at bottom
- Using arrow functions expecting hoisting
Ready to prove it?
Pass the test and this pattern moves unknown
