Synchronous Code Execution Order
You'll learn how JavaScript executes synchronous code line-by-line in a completely predictable, top-to-bottom order.
The Core Idea
When you write synchronous JavaScript code, the engine reads and executes each statement in the exact order it appears in your file—no surprises, no jumping around. The first line runs to completion, then the second, then the third, and so on. Each statement must finish before the next one begins.
Think of it like reading a recipe: you complete step 1, then step 2, then step 3. You can't skip ahead or do step 5 while step 2 is still in progress.
How It Works
console.log("First");
console.log("Second");
console.log("Third");
Output:
First
Second
Third
There's no mystery here. Line 1 executes completely, then line 2, then line 3. The order is guaranteed.
Even with function calls:
function greet() {
console.log("Hello");
}
console.log("Start");
greet();
console.log("End");
Output:
Start
Hello
End
When you call greet(), JavaScript jumps into that function, executes everything inside it top-to-bottom, then returns to continue with the next line after the function call.
Why This Matters
Understanding synchronous execution order is the foundation for understanding why JavaScript behaves the way it does. Every line blocks the next line from running until it's done. This predictability makes synchronous code easy to reason about—but as you've already learned, it also creates problems when operations take a long time (blocking the main thread).
Key Takeaway: Synchronous JavaScript code executes in strict top-to-bottom order, with each statement blocking the next until it completes.