Synchronous vs Asynchronous Programming

🔹 Synchronous JavaScript (Sync)

Synchronous programming means each operation is executed one after another, in a sequential manner. If one task takes time, it blocks the execution of the next task until it completes.

Example of Synchronous Code:

console.log("Task 1");
console.log("Task 2");
console.log("Task 3");

🔍 Output:

Task 1
Task 2
Task 3

➡️ The above code executes line by line in the same order.

The following image shows sync way of loading images :

These images load one by one which is example of executing tasks in sync way.

These images load one by one which is example of executing tasks in sync way.

🚨 Problem with Synchronous Code :

Imagine if one of the tasks takes a long time (e.g., fetching data from a server). Everything else would pause until that task completes, causing delays.