JavaScript runs on one thread, yet it handles timers, network requests, and clicks without freezing the page. The trick is not magic and not extra threads in your code. It is a loop that keeps checking for work, running what is ready, and parking the rest until its turn comes. Once you see how the JavaScript event loop moves jobs around, timers that fire late, promises that resolve in a strange order, and pages that freeze all start to make sense.
This guide walks through the loop piece by piece: the call stack where code runs, the queues where waiting work sits, and the order the loop follows when it picks the next job. Every example is short, and you can run each one in a console or a Node script to watch the order for yourself.
How the JavaScript Event Loop Moves Work
The loop itself is simple. It looks at the call stack, and if the stack is empty, it takes the next job from a queue and runs it. Then it checks again, and again, for as long as the program lives. That cycle is the event loop. It never runs your code in parallel with itself. It runs one job at a time, in order, and the order of the queues decides which job goes first.
Three parts matter here. The call stack holds the function that is running right now, plus the functions that called it. The task queue holds callbacks from timers, network responses, and events. The microtask queue holds promise callbacks, and it gets priority: the loop empties it before it touches the task queue.
The Call Stack: Where Code Actually Runs
The call stack is a pile of function calls. When a function runs, it goes on top of the pile. When it calls another function, that one goes on top of it. When a function returns, it comes off. Code runs from the top of the pile, one function at a time, and nothing else runs until the pile is empty.
A call stack event loop diagram would show the loop waiting while the stack has work in it. That is the key point: timers and promise callbacks cannot run while your code is still busy. A timer set for 100 milliseconds fires at 100 milliseconds only if the stack is free then. If your code is still grinding through a long loop, the timer waits. This is why a page freezes during a heavy calculation: the loop cannot get to the click handler or the paint job until the stack clears.
function first() {
second();
console.log("first");
}
function second() {
console.log("second");
}
first();
// prints "second", then "first"
Nothing here involves the loop yet. It is plain stack order, and it is worth getting comfortable with before adding queues.
Task Queue, Microtask Queue, and Why Promises Jump Ahead
When a timer finishes or a network request comes back, its callback does not run at once. It waits in the task queue until the stack is empty and the loop picks it up. Promise callbacks wait in a different line, the microtask queue, and the loop drains that line completely before it takes even one job from the task queue.
Run this example and read the order before you look at the answer:
console.log("1");
setTimeout(function () {
console.log("2");
}, 0);
Promise.resolve().then(function () {
console.log("3");
});
console.log("4");
// prints 1, 4, 3, 2
1 and 4 print first because they run straight off the stack. The promise callback prints next because the microtask queue drains before the task queue. The timer prints last even though its delay was zero, because a zero delay means “as soon as you can”, not “right now”. A timer callback always waits behind every microtask that is already queued.
That rule has a sharp edge. A microtask that keeps adding more microtasks can starve the task queue, so timers and clicks wait while the loop drains an endless line of promises. In normal code this never happens, but a loop that chains promises without a break can do it.
Event Loop Phases Explained for Node.js
In the browser the loop is roughly what this guide has described so far. In Node.js the loop has named phases, and each phase handles one kind of work. Event loop phases explained in plain terms: timers run first, then pending callbacks, then the poll phase where new I/O events wait, then the check phase for setImmediate, then close callbacks. Microtasks run between every phase, not just at the end, so a promise callback can slip in at many points.
Two details catch people out. First, setTimeout and setInterval run in the timers phase, while setImmediate runs in the check phase, which comes after the poll phase. Inside an I/O callback, setImmediate always runs before a zero-delay timer, because the loop is already past the timers phase for that round. Second, process.nextTick runs before promise callbacks, and a loop of nextTick calls can starve the loop the same way endless microtasks can.
const fs = require("fs");
fs.readFile("package.json", function () {
setTimeout(function () { console.log("timer"); }, 0);
setImmediate(function () { console.log("immediate"); });
});
// prints "immediate", then "timer"
Inside the file callback, the order is stable: immediate first, timer second. If you build a small REST API with Node.js and Express, this is the loop your request handlers run on, and the phases are why a slow handler delays every request behind it, not just its own.
JavaScript Asynchronous Execution Without Threads
JavaScript asynchronous execution looks like multitasking, but your code never runs on two threads at once. The waiting happens outside your code: the browser or Node watches the network and the clock while the loop does other work. When a response arrives, its callback joins a queue and runs when the loop reaches it. The async part is the waiting, not the running.
That split is why async and await read like straight-line code but behave like callbacks underneath. An await pauses one function and hands control back to the loop, which runs other jobs until the awaited promise settles. The function resumes as a microtask. Nothing about the pause lets two functions run at the same instant; it only lets the loop use the waiting time.
async function fetchUser() {
console.log("start");
await Promise.resolve();
console.log("after await");
}
fetchUser();
console.log("outside");
// prints "start", "outside", "after await"
The log after the await waits because the rest of the function becomes a microtask. Code after the call to fetchUser runs first, since it is still on the stack. When you deploy a Node.js app to a VPS with PM2 and Nginx, this same single-threaded loop serves every request, which is why one slow synchronous job can stall the whole server.
What Blocks the Loop and How to Spot It
The loop only blocks when code on the stack takes too long. Parsing a huge JSON file, looping over millions of rows, or running a heavy regex on a long string all hold the stack, and while they hold it, nothing else runs: no clicks, no timers, no network callbacks, no screen updates. The fix is to move the work off the stack, either by splitting it into chunks with setTimeout between them, by running it in a worker thread, or by caching the result so the work happens once. A beginner guide to Redis caching shows the caching route, which fits many read-heavy endpoints.
Spotting a blocked loop is straightforward. If clicks feel dead while a job runs, or timers fire late by seconds instead of milliseconds, the stack is busy. In the browser, the performance tab of the developer tools shows long tasks on the main thread with their exact length. In Node, a slow endpoint under load points the same way. The cure is rarely a bigger server. It is usually smaller chunks of work, or work moved out of the request path entirely.
Frequently Asked Questions
Is JavaScript really single-threaded?
Your code runs on one thread. The browser and Node run other threads behind the scenes for network, timers, and file access, but your callbacks always run one at a time on the main thread.
Why does setTimeout with zero delay not run at once?
A zero delay puts the callback at the back of the task queue. It runs after the current stack clears and after every microtask that is already waiting. Zero means no extra wait, not no wait at all.
Do web workers change how the event loop works?
Workers give you extra threads with their own loops, but they cannot touch the page or share variables with the main thread. They pass messages back and forth, and each message arrives through the main loop like any other event.