How JavaScript Works
Written by
Saleh Kamal
Web Developer
Saleh Kamal Web dev focus on frontend and web performance
PortfolioWritten by
Web Developer
Saleh Kamal Web dev focus on frontend and web performance
PortfolioJavaScript confuses a lot of developers for one specific reason: we tend to mix two distinct concepts together—the core language and the runtime environment.
If you have ever wondered why setTimeout isn't technically part of JavaScript, or how a single-threaded language handles asynchronous tasks, you are in the right place. In this guide, we'll break down exactly how JavaScript works—in the simplest way possible.
The core language is the part defined by the ECMAScript specification. This is the "basic stuff" you learn when you first start coding.
The core language includes:
These elements are JavaScript itself. They do not change, regardless of whether you run your code in a Chrome browser, on a Node.js server, or on a smart toaster.
The Runtime Environment is where things get interesting. The runtime depends entirely on where JavaScript is running, and it gives the language extra abilities it doesn't have on its own.
To understand this, think of JavaScript as a Professional Chef.
If JavaScript is running in the browser, the browser is the restaurant. The browser runtime is the kitchen. The tools inside that kitchen are things like the DOM API (to change HTML) or the Fetch API (to get data).
Key Takeaway: These tools are not JavaScript. They are provided by the runtime.
This distinction becomes clearer when we move our "Chef" to a different restaurant: the Server.
When JavaScript runs in Node.js, it gets a completely different set of tools. It doesn't need to manipulate HTML (the DOM), so that tool is gone. Instead, Node.js gives JavaScript:
The core language (loops, variables) stays the same, but the "kitchen tools" change completely.
The browser runtime is the most common environment developers struggle with. It is composed of four main parts working in harmony.
This is the program that converts your human-readable code into machine instructions.
These are the browser tools we mentioned earlier.
setTimeout, setInterval)Note: When you use
setTimeout, you aren't using a JavaScript feature; you are using a browser feature.
When Web APIs finish their work (like a timer finishing or data returning from a server), they don't interrupt the code immediately. They push their instructions (callbacks) into the Callback Queue. This queue patiently waits for JavaScript to be free.
The Event Loop is the traffic controller. It has one simple job. It constantly asks:
If the stack is empty, it moves the next callback from the queue into the stack. This is how JavaScript achieves asynchronous behavior despite being single-threaded.
Let's look at a classic interview question to see this in action. Imagine you run this code:
console.log("1"); setTimeout(() => console.log("2"), 0); console.log("3");
You might expect the output to be 1, 2, 3. However, the real output is:
1 3 2
Why does this happen?
console.log("1") goes onto the Call Stack and runs instantly.setTimeout is sent to the Web APIs. JavaScript does not wait for it.console.log("3") immediately.console.log("2") callback onto the stack to be executed.We have:
All working together to give a single-threaded language asynchronous superpowers.
Comments
Please sign in or create an account to comment.
No comments yet. Be the first to share your thoughts!