DEV Community

Bala Murugan
Bala Murugan

Posted on

Behind the Scenes of JavaScript: Synchronous vs Asynchronous Explained Simply

What is Synchronous JavaScript

Synchronous means code runs line by line, one after another.
Next line waits until the previous line finishes.

Example:

console.log("Start");

function slowTask() {
  for (let i = 0; i < 1e9; i++) {} // takes time
  console.log("Task Done");
}

slowTask();
console.log("End");

Output:
Start
Task Done
End
Enter fullscreen mode Exit fullscreen mode

Explanation:

Here, End waits until slowTask finishes. This is Synchronous.

What is Asynchronous JavaScript?

Asynchronous means code does NOT wait.
It runs tasks in background and continues next lines.

Example:

console.log("Start");

setTimeout(() => {
  console.log("Task Done");
}, 2000);

console.log("End");

Output:
Start
End
Task Done
Enter fullscreen mode Exit fullscreen mode

Explanation:

Here, End does NOT wait → This is Asynchronous.
Enter fullscreen mode Exit fullscreen mode

Why Asynchronous is Needed :

Because JavaScript is single-threaded (one task at a time).
Async helps to handle:

API calls
Database calls
File reading
Timers
User actions

Without async → Website will freeze.

Asynchronous Methods in JavaScript:

There are 3 main ways:

Method Use
Callback Old method
Promise Better
Async/Await Best & modern

Callback Example:

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

fetchData(() => {
  console.log("Process data");
});
Enter fullscreen mode Exit fullscreen mode

Async/Await Example (Best Method):

function fetchData() {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve("Data fetched");
    }, 2000);
  });
}

async function getData() {
  let data = await fetchData();
  console.log(data);
}

getData();

Enter fullscreen mode Exit fullscreen mode

Simple Difference Table

| Synchronous | Asynchronous |
| ----------------- | ------------------------ |
| Runs line by line | Runs in background |
| Waits | Does not wait |
| Blocking | Non-blocking |
| Slow | Fast |
| Example: for loop | Example: setTimeout, API |

Real Life Example

Synchronous:
*You stand in line and wait for tea → then go to next work.

Asynchronous:

   *You order tea → while tea is preparing, you do another work → then drink tea.
Enter fullscreen mode Exit fullscreen mode

Event Loop Diagram

      ┌───────────────┐
    │   Call Stack   │
    └───────────────┘
            ↑
            │
    ┌───────────────┐
    │   Event Loop   │
    └───────────────┘
            ↑
            │
    ┌───────────────┐
    │ Callback Queue │
    └───────────────┘
            ↑
            │
    ┌───────────────┐
    │   Web APIs     │
    │ setTimeout     │
    │ API Calls      │
    │ DOM Events     │
    └───────────────┘
Enter fullscreen mode Exit fullscreen mode

Explanation:

*Call Stack → Executes code
*Web APIs → Handle async work
*Callback Queue → Stores completed async tasks
*Event Loop → Moves task to Call Stack

Top comments (0)