Script Valley
JavaScript in the Browser: DOM, Events & APIs
Asynchronous JavaScript and the Fetch APILesson 4.1

What is asynchronous JavaScript and why does it matter

synchronous vs asynchronous, call stack, Web APIs, callback queue, event loop, non-blocking IO

Synchronous vs Asynchronous JavaScript

JavaScript event loop

JavaScript runs on a single thread. Synchronous code runs line by line - each line blocks the next until it completes. A long synchronous operation (fetching a file, computing a large dataset) freezes the browser entirely.

Asynchronous code starts an operation, hands it off to the browser's Web APIs, and moves on. When the operation finishes, a callback is placed in the callback queue. The event loop picks it up and runs it only when the call stack is empty.

console.log('1 - start');

setTimeout(() => {
  console.log('3 - timeout callback');
}, 0);

console.log('2 - end');
// Output: 1, 2, 3 - even with 0ms delay

Why This Matters for the Browser

Network requests, file reads, and timers are all asynchronous. If they were synchronous, the browser UI would freeze until the server responded. Asynchronous JavaScript keeps the UI responsive while waiting for data.

The event loop also explains why async/await does not literally pause the thread - the stack is simply freed and the callback resumes later.

Up next

JavaScript Promises: then, catch, and Promise.all

Sign in to track progress