Facebook Pixel

Is JavaScript Synchronous or Asynchronous?

JavaScript is synchronous by design. Here is how the event loop and Web APIs create the illusion of async behavior.

Is JavaScript Synchronous or Asynchronous?

JavaScript is synchronous and single-threaded by design. It executes one statement at a time, in order. But it can behave asynchronously because of the runtime around it.

The Core Truth

The JS engine has one call stack. Only one function runs at a time. If that function takes 5 seconds, the UI freezes for 5 seconds. That is synchronous behavior.

Where Async Comes From

Async behavior comes from the runtime environment, not the language itself:

  • Web APIs (in browsers): setTimeout, fetch, DOM events, geolocation.
  • Node.js APIs: fs, http, crypto.

These APIs run outside the call stack. When their work is done, they push callbacks into a queue. The event loop moves those callbacks into the call stack only when the stack is empty.

The Components

  • Call Stack: where JS code runs, one at a time.
  • Web APIs: browser-provided async operations.
  • Callback Queue (Macrotask Queue): callbacks from setTimeout, events.
  • Microtask Queue: callbacks from promises, higher priority.
  • Event Loop: moves queued callbacks to the stack when it is empty.

A Common Misconception

setTimeout(cb, 0) does not run cb immediately. It puts cb in the callback queue. The event loop runs it only after the current stack and all microtasks finish. So JS itself is synchronous; the runtime makes it appear async.

Why This Matters

If you call a long-running synchronous function (like a heavy loop), timers and clicks will not fire until it finishes. To keep the UI responsive, push expensive work off the main thread using Web Workers, or break it into chunks with setTimeout or requestAnimationFrame.

The Takeaway

JS is synchronous and single-threaded at the language level. Asynchronous behavior is provided by the runtime's Web APIs, queues, and the event loop, which schedule callbacks to run later on the single call stack.

JavaScript is synchronous and single-threaded. It executes one statement at a time. Asynchronous behavior comes from the runtime's Web APIs and the event loop, not the language itself.

Because cb goes to the callback queue. The event loop only moves it to the call stack after the current stack is empty and microtasks are done. JS is synchronous, so the timer cannot interrupt running code.

A mechanism that continuously checks if the call stack is empty. If it is, it moves callbacks from the microtask queue (then macrotask queue) onto the stack for execution.

Single-threaded. There is one call stack, so only one piece of code runs at a time. Web Workers provide separate threads but they do not share the call stack.

Through Web APIs (setTimeout, fetch, events) that run outside the engine. When done, they queue callbacks. The event loop pushes those callbacks onto the single call stack when it is free.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.