Skip to main content

Command Palette

Search for a command to run...

Synchronous vs Asynchronous JavaScript

Sneak peek into an important JS concept.

Published
•4 min read
Synchronous vs Asynchronous JavaScript
G
I enjoy blending technology with business to build solutions that create real impact. I’m fascinated by how technology empowers people, businesses, and startups. I believe thoughtful use of technology quietly improves lives, and that belief fuels everything I build and explore 💻.

If you’ve been writing JavaScript for some time, you must have seen cases where code runs line by line exactly as written, and then there are cases where something starts but finishes later. That is where synchronous and asynchronous behavior comes into picture. At first it feels like JavaScript is behaving inconsistently, but in reality there is a clear pattern behind it. Once that pattern is understood, things become predictable.The idea is simple. JavaScript by default runs code in a synchronous way, which means one line executes, then the next, then the next. Nothing jumps ahead, nothing waits in between unless something explicitly introduces that behavior.

What synchronous code means

Synchronous code means the code runs step by step in order. Each line waits for the previous one to finish before it starts executing. There is no skipping, no delay handling, just straight execution.

console.log("Start");

function task() {
  console.log("Task running");
}

task();

console.log("End");

Here the output will always be:

**Start

*Task running

**End

This is because each line completes before moving to the next one. This is called blocking behavior because if one line takes time, everything after it has to wait.

Blocking problem in synchronous code

Now the issue with synchronous code shows up when a task takes time. Suppose there is a heavy operation or something that takes a few seconds, everything after it gets blocked.

console.log("Start");

function heavyTask() {
  for (let i = 0; i < 1e9; i++) {}
  console.log("Heavy task done");
}

heavyTask();

console.log("End");

Here End will only print after the loop finishes. So the entire program is stuck until that work is done. This is what blocking looks like, nothing else can move forward.

What asynchronous code means

Asynchronous code allows certain tasks to run in the background without blocking the main execution. That means JavaScript can start something, move ahead, and then come back to it later when it is done.
So instead of waiting, it continues executing the next lines.

console.log("Start");

setTimeout(() => {
  console.log("Async task done");
}, 2000);

console.log("End");

The output will be:

**Start

**End

*Async task done

Even though the timeout was written in between, it runs later. This is non blocking behavior.

Why JavaScript needs asynchronous behavior

JavaScript runs on a single thread. That means it can do one thing at a time. If everything was synchronous, then any slow operation like fetching data from an API or reading a file would block the entire application.
Imagine clicking a button on a website and the UI freezes because it is waiting for data. That would be a bad experience. So JavaScript uses asynchronous behavior to handle these situations without stopping everything else.
This allows things like UI updates, user interactions, and other operations to continue smoothly.

Example with API like behavior

A very common real world case is fetching data. Even though we are not actually calling an API here, the behavior can be understood like this.

console.log("Start");

function fetchData() {
  setTimeout(() => {
    console.log("Data received");
  }, 2000);
}

fetchData();

console.log("End");

Again, the output will be:

**Start

**End

*Data received

So JavaScript did not wait for the data, it moved ahead and handled it later.

Blocking vs non blocking clarity

In synchronous code, each line blocks the next one until it finishes. In asynchronous code, certain operations are handed off and JavaScript continues executing other lines.
This is why asynchronous code is called non blocking, because it does not stop the flow of execution.

What is happening behind the scenes

When an asynchronous task like setTimeout is called, it is not executed immediately by the main thread. It is handled separately and once it is ready, it is pushed back to be executed.
That is why even if it appears in between, it runs after the main synchronous code finishes.

Final clarity

Synchronous code runs line by line and blocks execution until each step is complete. Asynchronous code allows certain tasks to run separately so that the main execution does not stop. This is important because JavaScript is single threaded and needs a way to handle slow operations without freezing everything.
So whenever you see something like timers, API calls, or delayed execution, that is JavaScript handling things asynchronously to keep the application responsive.