What hoisting is in Javascript - Time & Space Complexity
We want to understand how hoisting affects the order and cost of running JavaScript code.
Specifically, how does hoisting change when and how many times parts of code run?
Analyze the time complexity of the following code snippet.
function example() {
console.log(a);
var a = 5;
console.log(a);
}
example();
This code shows variable usage before and after declaration to illustrate hoisting.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The function runs once, with two console.log calls.
- How many times: Each console.log runs once per function call.
Since the function runs a fixed number of steps, the work does not grow with input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 2 console.log calls |
| 100 | 2 console.log calls |
| 1000 | 2 console.log calls |
Pattern observation: The number of operations stays the same no matter the input size.
Time Complexity: O(1)
This means the code runs in constant time, doing the same amount of work regardless of input.
[X] Wrong: "Hoisting makes the code run multiple times or loops behind the scenes."
[OK] Correct: Hoisting only moves declarations up before running code; it does not repeat or loop code.
Understanding hoisting helps you explain how JavaScript runs code step-by-step, a key skill for clear coding and debugging.
"What if we replaced var with let in the example? How would the time complexity change?"
Practice
Solution
Step 1: Understand hoisting concept
Hoisting means JavaScript moves declarations of variables and functions to the top of their scope before running the code.Step 2: Compare options
Only JavaScript moves variable and function declarations to the top before running code. correctly describes this behavior. Other options describe unrelated behaviors.Final Answer:
JavaScript moves variable and function declarations to the top before running code. -> Option BQuick Check:
Hoisting = Moving declarations up [OK]
- Thinking hoisting moves variable assignments, not just declarations
- Confusing hoisting with asynchronous code execution
- Believing hoisting applies to all variable types equally
Solution
Step 1: Identify hoisting behavior for variable types
Variables declared withvarare hoisted and initialized asundefined.letandconstare hoisted but not initialized, causing a temporal dead zone.Step 2: Match options to behavior
var z = 15; usesvar, so it is hoisted and initialized asundefined. Others do not behave this way.Final Answer:
var z = 15; -> Option DQuick Check:
var hoisted as undefined [OK]
- Assuming let and const behave like var with hoisting
- Confusing function declarations with variable declarations
- Thinking function expressions are hoisted like functions
console.log(myVar); var myVar = 10;
Solution
Step 1: Understand var hoisting with initialization
The declarationvar myVaris hoisted to the top, but the assignment= 10stays in place.Step 2: Evaluate console.log output
At the time ofconsole.log(myVar),myVarexists but isundefinedbecause assignment hasn't happened yet.Final Answer:
undefined -> Option AQuick Check:
var declaration hoisted, value undefined [OK]
- Expecting the value 10 to print immediately
- Confusing ReferenceError with undefined for var
- Thinking let or const behave the same way
console.log(foo());
var foo = function() { return 'Hello'; };Solution
Step 1: Understand hoisting of function expressions
Variablefoois hoisted asundefined, but the function assignment is not hoisted.Step 2: Analyze calling foo before assignment
Callingfoo()before assignment tries to callundefinedas a function, causing a TypeError.Final Answer:
TypeError: foo is not a function -> Option CQuick Check:
Function expressions not hoisted as functions [OK]
- Thinking function expressions are fully hoisted
- Expecting no error because of hoisting
- Confusing ReferenceError with TypeError
console.log(greet());
function greet() {
return 'Hi!';
}
var greet = function() {
return 'Hello!';
};
console.log(greet());What will be the output?
Solution
Step 1: Analyze hoisting of function declaration
The function declarationfunction greet()is fully hoisted, so the firstgreet()call returns 'Hi!'.Step 2: Analyze var assignment after first call
Thevar greetdeclaration is hoisted but initialized as undefined. The assignment to a function expression happens after the first call, so the secondgreet()call uses the new function returning 'Hello!'.Final Answer:
'Hi!' then ReferenceError -> Option AQuick Check:
Function declaration hoisted, var assignment overrides later [OK]
- Thinking var assignment overrides before first call
- Expecting ReferenceError due to var redeclaration
- Confusing function declaration and expression hoisting
Ready to prove it?
Pass the test and this pattern moves unknown
