All 28 lessons ▾

Inside JavaScript · Lesson 2 of 28

Two Passes and the Call Stack: How Your Code Really Executes

Updated 16 Nov 2026 · By Akshay, software engineer, India · 8 min read

When you run your JavaScript code, a lot happens behind the scenes, before your first line even executes. So what exactly happens? Let's see everything, step by step.

Got a doubt? Ask in the comments under the video.Subscribe

In one minute
  • Running a program creates the global execution context
  • Phase 1, memory: variables get undefined, functions stored whole
  • Phase 2, code: runs line by line, with the real values
  • Every call gets a brand new context, built in the same two phases
  • return hands back the value and control; then the box is deleted
  • The call stack: push on a call, pop on a return

Part 1What happens when you hit run?

Quick revision from the last episode. Remember the one rule? Everything in JavaScript happens inside an execution context. A big box, with two parts, memory and code. Now, here's today's big question. What actually happens the moment you run a JavaScript program? Think about it for a second.

THE ANSWER
Runningaprogramcreatestheglobal
executioncontext.
and it's built in two phases

Yes, you got it right. An execution context is created. The very first one, for your whole program, is called the global execution context. Global, because it holds everything at the top level of your file. And now listen to this very carefully, because most people miss this part. This box isn't filled in one go. It's created in two phases.

Part 2Two passes over your code

So let's see these two phases, one by one.

Phase 1 · Memory
make room for every name
a : undefined
sum : the whole
function
Phase 2 · Code
run it, line by line
a : 10
sum( ) runs

Phase one is called the memory creation phase. Some people just call it the creation phase. In this phase, JavaScript goes through your whole program, top to bottom, without running anything. It's only looking for names. Every variable it finds gets a spot in memory, and that spot holds a special placeholder value called undefined. Remember this word, undefined. It gets a whole episode of its own later on. And functions? Every function gets stored whole. The entire code of the function, exactly as you wrote it. Then comes phase two, the code execution phase. Now JavaScript goes through the program again, line by line, and this time, it actually runs it. Real values go into memory, calculations happen, and your functions finally run.

Before the guests
come
a name card on every chair
The guests arrive
each one takes their seat
= phase 1: reserved, still
undefined
= phase 2: real values

Let me give you a picture you'll never forget. Think of a wedding reception. Before a single guest arrives, someone walks around the hall and puts a name card on every chair. Every seat is reserved, but it's still empty. That's phase one. And that reserved but empty seat? That's undefined. Then the guests arrive, and each one sits in their own seat. That's phase two, when the real values take their places. Reserved first, filled later.

Part 3Watch both passes, line by line

Now let's run a real program, and watch both phases happen in front of your eyes.

sum.js
1var a = 10;2var b = 20;3 4function sum(x, y) {5  var total = x + y;6  return total;7}8 9var result = sum(a, b);10var result2 = sum(5, 7);

Here's our program for today. And look at the bottom: sum is called twice. Phase one, memory creation. JavaScript starts at the top. Line one, a. It gets undefined. Line two, b. Undefined. Line four is a function called sum, so the whole function goes into memory, exactly as it's written. Line nine, result. Undefined. Line ten, result two. Undefined too. And notice something very important. Not a single line has actually run yet. Memory is just full of reserved seats.

Now phase two. Code execution. JavaScript goes back to the top, and this time it runs every line. Line one. a equals ten. So undefined gets crossed out, and ten takes its seat. Line two. b becomes twenty. Then lines four to seven are the function. Is there anything to run there? No! It's already sitting in memory. So JavaScript skips straight to line nine.

Now, line nine is the most interesting line. It has sum, with round brackets after it. And whenever you see a function name with these round brackets, it means, run this function now. That's a function call. And here's something you must remember. A function is like a small program inside your program. So every time a function runs, JavaScript creates a brand new execution context, just for that call. A new box, with its own memory and its own code. And this new box is built in the same two phases. Phase one first. sum has two parameters, x and y, and one variable, total. All three get undefined.

Then phase two, inside the function. The values of a and b come in from line nine. So x becomes ten, and y becomes twenty. Now, quick vocabulary check, because interviewers love this one. x and y, the names written in the function, are called parameters. And ten and twenty, the values you pass in, are called arguments. Parameters, and arguments. Now line five. total equals x plus y, which is thirty.

Line six. return total. Now listen carefully, because this word return does two things. One, it hands back the value of total. And two, it hands control back to the line that called the function. And where does it find total? In the function's own memory. We call it the local memory. Now the function's job is done. So JavaScript deletes its execution context, completely. Gone! And control goes back to line nine, where result becomes thirty.

Next, line ten calls sum again, this time with five and seven written straight in. And here's a question for you. Does JavaScript reuse the old box? No! That one is gone. It creates another brand new execution context, and runs the same two phases all over again. Phase one, x, y and total get undefined. Phase two, x becomes five, y becomes seven, and total becomes twelve. return hands twelve back, this box gets deleted too, and result two becomes twelve.

And that was the last line. So the program is finished. And what happens to the global execution context now? It gets deleted too. Everything is cleaned up, and JavaScript is done with your program.

Part 4Who keeps track of all this?

Now, stop for a second, and think about what JavaScript just juggled.

Global
checkout()
getTotal()
addTax()
calls
calls
calls
...and deeper
THE ANSWER
JavaScriptusesastack:
thecallstack.
last in, first out

A global box. Then a box for sum, created and deleted. Then another box for sum. And that's a tiny program. In a real app, one function calls another, and that one calls another, and it can go many levels deep. Boxes inside boxes inside boxes. So how does JavaScript keep track of which box is running right now, and where to go back to when it's done? Tough job.

It manages all of this with a stack. And this stack is called the call stack. Picture a stack of plates at a buffet. You always put a new plate on top, and you always take the top plate off first. Last in, first out. The call stack works exactly like that. Except here, the plates are execution contexts.

Part 5Watch the call stack

So let's run our program one more time. And this time, keep your eyes on the call stack, on the far right.

sum.js
1var a = 10;2var b = 20;3 4function sum(x, y) {5  var total = x + y;6  return total;7}8 9var result = sum(a, b);10var result2 = sum(5, 7);

The moment the program starts, the global execution context is created, and pushed onto the call stack. It sits at the very bottom. And remember this, it stays there until the whole program ends. Then phase one fills memory, just like before.

Phase two. Line one and line two set a and b. Then line nine calls sum. And its brand new execution context is pushed on top of the stack. Now sum is running, and global is waiting underneath it. sum returns thirty, so its context is popped off the stack. And control drops back to global, right where it left off. Line nine, and result becomes thirty.

Line ten calls sum again. So another new context is pushed on top. It works out twelve, returns, and gets popped. Control goes back to global, and result two becomes twelve. And now the program is over, so global is popped too. And look, the call stack is empty. The run is done!

REMEMBER THIS
Thecallstackkeepstheorderof
executionofexecutioncontexts.
pushed on a call, popped on a return

So here's the one line to remember from this episode. The call stack maintains the order of execution of execution contexts. Let me say that again, slowly. The call stack keeps track of which execution context runs, and in what order. A function is called, its context is pushed. It returns, its context is popped. And global sits at the bottom the whole time.

Part 6Same stack, five more names

Now one last thing, because it confuses a lot of people.

Other names for the call stack
->
Execution context stack
->
Program stack
->
Control stack
->
Runtime stack
->
Machine stack
INTERVIEW ANSWER
It'sthestackJavaScript
usestomanageexecution
contexts.
global at the bottom, push on a call, pop on a return

Around the internet, you'll see the call stack called by other names. The execution context stack. The program stack. The control stack. The runtime stack. And the machine stack. Five names, and every single one of them means the same thing, the call stack. Don't get confused.

Homework

What would the call stack look like if sum called another function? Draw it, and tell me in the comments.

Show the answer

Say sum calls another function, double, before it returns:

1var a = 10;2var b = 20;3 4function double(n) {5  return n * 2;6}7 8function sum(x, y) {9  var total = x + y;10  return double(total);11}12 13var result = sum(a, b);

The call stack grows one box at a time:

  • Global sits at the bottom.
  • sum(10, 20) is called, so its context is pushed on top of Global.
  • Inside sum, double(30) is called, so its context is pushed on top of sum. Now there are three boxes: double, sum, Global. Only the top one, double, is running.
  • double returns 60. Its box is popped, and control goes back to sum.
  • sum returns 60. Its box is popped too, and result gets 60.
  • The program ends, and Global is popped. The stack is empty.

Try it first, then post yours in the comments under the video.

Your progress stays on this device.
A new lesson every day at 11 am

The video on YouTube, and the lesson as a swipe-through on Instagram.