Skip to main content

Command Palette

Search for a command to run...

Synchronous vs Asynchronous JavaScript

Updated
3 min read
Synchronous vs Asynchronous JavaScript

Hey! Imagine you're doing tasks one by one — like brushing your teeth, then eating breakfast, then checking your phone.

That’s basically how synchronous code works. But what if you could order food, and while waiting, watch a video? That’s asynchronous code.

Let’s break it down step by step in a super simple way 👇

What is Synchronous Code?

Synchronous code means everything runs line by line, one after another. JavaScript will wait for each task to finish before moving to the next one.

Example:

console.log("Start");
console.log("Middle");
console.log("End");

👉 Output:

Start
Middle
End

🧠 Simple Understanding:

It’s like standing in a queue — you can’t go ahead until the person before you is done.

Synchronous Timeline

[ Task 1 ] → [ Task 2 ] → [ Task 3 ]
   ↓            ↓            ↓
 Complete     Complete     Complete

👉 Everything happens in order, no skipping, no waiting in parallel.

What is Asynchronous Code?

Asynchronous code means JavaScript does NOT wait for some tasks. 👉 It starts a task, and if it takes time, it moves to the next task.

💡 Example:

console.log("Start");

setTimeout(() => {
  console.log("Waited 2 seconds");
}, 2000);

console.log("End");

👉 Output:

Start
End
Waited 2 seconds

🧠 Simple Understanding:

It’s like ordering food and watching YouTube while waiting 🍔📱

Why JavaScript Needs Asynchronous Behavior?

JavaScript is single-threaded (one thing at a time).

👉 If it didn’t support async:

  • App would freeze ❌

  • UI would stop responding ❌

  • Users would get frustrated ❌

💡 Real-life Example:

Imagine:

You click a button

  • It fetches data from the internet

  • It takes 5 seconds

👉 Without async: Everything would freeze for 5 seconds 😬

👉 With async: You can still scroll, click, interact 👍

Examples (API Calls & Timers)

🌐 API Call Example:

setTimeout(() => {
  console.log("Runs later");
}, 3000);

This runs after 3 seconds, but JS doesn't wait.

Asynchronous Flow (Event Loop Concept)

Start Task
   ↓
Async Task Started
   ↓
Next Task Runs Immediately
   ↓
Async Task Finishes Later

Problems with Blocking Code

Blocking code means everything stops until one task finishes.

🚨 Problems:

  • ❌ Slow performance

  • ❌ Bad user experience

  • ❌ UI freezes

  • ❌ App feels laggy

💡 Example of Blocking:

while(true) {
  // infinite loop
}

This will freeze the browser 😵

🧠 Real-life Example:

Imagine you're cooking and You wait 10 minutes just watching water boil,

instead of doing something else

👉 That’s blocking behavior — waste of time!

🔥 Blocking vs Non-Blocking

Type Behavior
Synchronous Blocking
Asynchronous Non-blocking

👉 Blocking = wait

👉 Non-blocking = move ahead and come back later


🎯 Conclusion

Synchronous code runs step by step, while asynchronous code allows tasks to run without waiting. JavaScript uses asynchronous behavior to keep applications fast and responsive. Without it, modern web apps would freeze and feel slow. Understanding this concept helps in writing efficient and user-friendly code.