All 28 lessons ▾
Inside JavaScript · Lesson 1 of 28
How JavaScript Runs Your Code: Execution Context
How does JavaScript actually run your code? Is it synchronous, or asynchronous? Single-threaded, or multi-threaded? Interviewers love these questions. So let's answer all three, properly.
Got a doubt? Ask in the comments under the video.Subscribe
- Everything in JavaScript happens inside an execution context
- Memory = variable environment (key : value)
- Code = thread of execution, one line at a time
- Synchronous + single-threaded: one thing, in order
- A slow line blocks every line after it
- Waiting goes to the browser; the event loop brings it back
Part 1The one rule
Before we write a single line of code, listen to this very carefully. Because this one rule is the base of this whole course. Everything else we learn sits on top of it.
Everything in JavaScript happens inside an execution context. I'll say it again, because it's that important. Everything, and I mean everything in JavaScript, happens inside an execution context. Every variable, every function, every single line you write. Now, execution context sounds like a scary word. It isn't. Just think of it as a big box. While your code is running, it lives inside this box. A big box. Remember this word. Execution context.
Part 2Inside the box
So what does this box look like on the inside? Let's open it up and draw it, piece by piece. And why should you care about this box? Because almost every confusing thing in JavaScript, a variable that says undefined, a function that works before you even write it, starts making sense the moment you can picture this box. So let's draw it.
So here's our box. And this box has exactly two parts. On the left side, memory. And on the right side, code. Just two parts. Everything JavaScript does happens across these two. So let's take them one at a time.
First, memory. This is where JavaScript keeps all your variables and functions. And it keeps them as key value pairs. So what does that mean? Say you write var a equals ten. The key is a, and the value is ten. Simple. And functions live here too. The key is the function's name, like greet, and the value is the whole function itself. Now this memory part has an official name. Listen carefully, because interviewers use this name. The variable environment. Memory is also called the variable environment. Think of it like the contacts app on your phone. A name, and the number saved against it. Key, and value.
Now the right side. Code. This is the place where your code actually runs. And here's the key thing. It runs one line at a time. The first line runs, and finishes. Then the second line. Then the third. Never two lines together. And just like memory, this part also has an official name. The thread of execution. Say it with me. Thread of execution. Picture one single thread, going through your code from top to bottom, one line after another.
So let's put it together. The execution context is a box with two parts. Memory, which is also called the variable environment. And code, which is also called the thread of execution. If you understood just this much, you're already ahead of most people. Remember these two names. You will hear them again and again in this course, and in your interviews too.
Part 3Rule number two
Now, here's the second rule. And this one answers two of our three questions in one shot. Listen to this one very carefully.
JavaScript is a synchronous, single-threaded language. Let me say it once more. JavaScript is synchronous, and JavaScript is single-threaded. Two big words. So let's break them down, one by one.
Single-threaded means JavaScript has only one thread. So it can do only one thing at a time. And synchronous means it does those things in order. It moves to the next line only when the current line is completely done. Think of a shop with just one billing counter. One cashier, one queue. The cashier finishes the first customer, then takes the second, then the third. Nobody gets billed together, and nobody skips the line. That's exactly how JavaScript runs your code. In some other languages, like Java, you can start many threads and really do several things at the same time. JavaScript gives you just one main thread. One cashier.
Part 4See it run
Enough theory. Let's see this with real code. And keep your eyes on the console, because that's where the proof is.
1console.log("Order placed");2console.log("Payment done");3console.log("Food on the way");Order placed Payment done Food on the way
Here's a tiny program. Three lines, three messages. Now watch the console, on the right. Line one runs first, and it prints order placed. Only when line one is completely done, does line two start. Line two, and it prints payment done. And then line three, and it prints food on the way. Notice something. JavaScript never decides to run line three first because it looks shorter. It simply goes top to bottom. Always this order. One, then two, then three. Never two lines at the same time, and never out of order. That's synchronous and single-threaded, right in front of your eyes.
1console.log("Start");2 3// a slow line: counts to a billion4for (let i = 0; i < e9; i++) {}5 6console.log("End");Start End
Now let's make it interesting. Quick question for you. What if a line is slow? Look at this program. Line one prints start. Then line four counts all the way to a billion. That takes a little while. So what happens to the last line in the meantime? Does it jump ahead? No! It waits. JavaScript can't move on, because the current line isn't done yet. Only when the counting is completely over, does it finally reach line six, and print end. That's what synchronous really means. Every line waits for the line before it. And now you also know why a slow line can freeze a whole web page. Remember this, it'll come back when we talk about the event loop.
Part 5But what about async?
Now, I know exactly what you're thinking right now. Something doesn't add up, does it?
You've heard of AJAX. And the A in AJAX stands for asynchronous. It's right there in the name! So how can a synchronous, single-threaded language do asynchronous things? Don't worry, here's the short version. JavaScript itself still runs one line at a time. When you ask it to wait, for a timer, or a network call, it hands that waiting to the browser, or to Node. So JavaScript never gets stuck. And when the answer is ready, something called the event loop brings it back. We'll go deep into that later in this course, with its own episode. For now, just remember this. JavaScript runs one thing at a time, and the browser helps with the waiting.
Which of these two rules was new for you?
Answer in the comments under the video.
The video on YouTube, and the lesson as a swipe-through on Instagram.