JavaScript execution flow shows how the computer reads and runs your code step by step. It helps you understand what happens first, next, and last.
JavaScript execution flow
Introduction
Syntax
Javascript
JavaScript runs code from top to bottom, line by line.
Functions run only when called.
Asynchronous code runs later, after current code finishes.JavaScript reads and runs your code in order, unless told otherwise by functions or special commands.
Understanding this flow helps you predict what your program will do.
Examples
Javascript
console.log('First'); console.log('Second'); console.log('Third');
Javascript
function greet() { console.log('Hello!'); } console.log('Start'); greet(); console.log('End');
Javascript
console.log('Before timeout'); setTimeout(() => { console.log('Inside timeout'); }, 1000); console.log('After timeout');
Sample Program
This program shows how JavaScript runs code top to bottom, but functions run only when called.
Javascript
console.log('Step 1: Start'); function stepTwo() { console.log('Step 2: Inside function'); } stepTwo(); console.log('Step 3: End');
Important Notes
Remember, functions do not run when defined, only when called.
Asynchronous code like setTimeout runs after the main code finishes.
Understanding execution flow helps you avoid surprises in your program.
Summary
JavaScript runs code line by line from top to bottom.
Functions run only when you call them.
Asynchronous code runs later, after current code finishes.
Practice
1. What happens when JavaScript encounters a function declaration in the code?
easy
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]
Hint: Functions run only when called, not on declaration [OK]
Common Mistakes:
- Thinking functions run immediately on declaration
- Confusing declaration with invocation
- Assuming functions are ignored until called
2. Which of the following is the correct way to call a function named
greet in JavaScript?easy
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]
Hint: Call functions with parentheses: name() [OK]
Common Mistakes:
- Forgetting parentheses when calling functions
- Using 'call' keyword incorrectly
- Confusing declaration syntax with call
3. What will be the output of the following code?
console.log('Start');
setTimeout(() => console.log('Timeout'), 0);
console.log('End');medium
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]
Hint: setTimeout runs after current code finishes [OK]
Common Mistakes:
- Assuming setTimeout runs immediately
- Mixing order of console logs
- Ignoring asynchronous behavior
4. Find the error in this code snippet:
function sayHi() {
console.log('Hi');
}
sayHimedium
Solution
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]
Hint: Always use () to call functions [OK]
Common Mistakes:
- Forgetting parentheses to invoke functions
- Thinking function name alone calls it
- Assuming no error when function not called
5. Consider this code:
What is the correct order of output?
console.log('1');
setTimeout(() => console.log('2'), 100);
Promise.resolve().then(() => console.log('3'));
console.log('4');What is the correct order of output?
hard
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]
Hint: Promise.then runs before setTimeout even with 0 delay [OK]
Common Mistakes:
- Thinking setTimeout runs before Promise.then
- Ignoring microtask queue priority
- Assuming all async run in order scheduled
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
