JavaScript execution flow - Time & Space Complexity
We want to understand how JavaScript runs code step by step and how long it takes as the code grows.
What happens to the time it takes when we add more instructions or loops?
Analyze the time complexity of the following code snippet.
function printNumbers(n) {
for (let i = 0; i < n; i++) {
console.log(i);
}
}
printNumbers(5);
This code prints numbers from 0 up to n-1 using a simple loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that runs console.log repeatedly.
- How many times: It runs exactly n times, once for each number from 0 to n-1.
As n grows, the number of times the loop runs grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 console.log calls |
| 100 | 100 console.log calls |
| 1000 | 1000 console.log calls |
Pattern observation: The work grows directly with n; doubling n doubles the work.
Time Complexity: O(n)
This means the time to run the code grows in a straight line with the input size.
[X] Wrong: "The loop runs faster because console.log is quick, so time doesn't depend on n."
[OK] Correct: Even if console.log is fast, the loop still runs n times, so more input means more steps.
Understanding how code runs step by step helps you explain your solutions clearly and shows you know how to think about program speed.
"What if we added a nested loop inside the first loop? How would the time complexity change?"
Practice
Solution
Step 1: Understand function declaration behavior
When JavaScript sees a function declared, it saves it in memory but does not execute it immediately.Step 2: Recognize when functions run
Functions only run when they are explicitly called later in the code.Final Answer:
The function is stored but not run until called. -> Option BQuick Check:
Functions run only when called = A [OK]
- Thinking functions run immediately on declaration
- Confusing declaration with invocation
- Assuming functions are ignored until called
greet in JavaScript?Solution
Step 1: Identify function call syntax
In JavaScript, calling a function requires the function name followed by parentheses.Step 2: Check each option
greet(); usesgreet();which correctly calls the function. Others are incorrect syntax.Final Answer:
greet(); -> Option AQuick Check:
Function call needs parentheses = A [OK]
- Forgetting parentheses when calling functions
- Using 'call' keyword incorrectly
- Confusing declaration syntax with call
console.log('Start');
setTimeout(() => console.log('Timeout'), 0);
console.log('End');Solution
Step 1: Understand synchronous and asynchronous code
console.log('Start') runs first and prints 'Start'. setTimeout schedules a function to run later, after current code finishes.Step 2: Trace the execution order
console.log('End') runs immediately after 'Start'. Then the setTimeout callback runs, printing 'Timeout'.Final Answer:
Start
End
Timeout -> Option AQuick Check:
Async runs after sync code = D [OK]
- Assuming setTimeout runs immediately
- Mixing order of console logs
- Ignoring asynchronous behavior
function sayHi() {
console.log('Hi');
}
sayHiSolution
Step 1: Check function call syntax
The code writessayHiwithout parentheses, so the function is not called.Step 2: Identify the fix
Adding parenthesessayHi()calls the function and prints 'Hi'.Final Answer:
Missing parentheses to call the function -> Option CQuick Check:
Function call needs () = C [OK]
- Forgetting parentheses to invoke functions
- Thinking function name alone calls it
- Assuming no error when function not called
console.log('1');
setTimeout(() => console.log('2'), 100);
Promise.resolve().then(() => console.log('3'));
console.log('4');What is the correct order of output?
Solution
Step 1: Understand synchronous, microtask, and macrotask queues
console.log('1') and console.log('4') run immediately (sync). Promise.then callbacks run next (microtask). setTimeout callbacks run last (macrotask).Step 2: Trace output order
Output order is '1' (sync), '4' (sync), '3' (Promise microtask), then '2' (setTimeout macrotask).Final Answer:
1
4
3
2 -> Option DQuick Check:
Promise microtasks before setTimeout macrotasks = B [OK]
- Thinking setTimeout runs before Promise.then
- Ignoring microtask queue priority
- Assuming all async run in order scheduled
Ready to prove it?
Pass the test and this pattern moves unknown
