JavaScript can execute code in two different ways:
- Synchronous JavaScript
- Asynchronous JavaScript
The main difference is how JavaScript handles the execution of tasks.
1. Synchronous JavaScript
Synchronous means that JavaScript executes code one statement at a time, in order.
The next statement will execute only after the previous statement has finished.
For example:
console.log("Step 1");
console.log("Step 2");
console.log("Step 3");
Output:
Step 1
Step 2
Step 3
Here, JavaScript executes the statements one after another.
Step 1
↓
Step 2
↓
Step 3
If one task takes a long time, the following tasks have to wait.
Real-World Example
Imagine you are standing in a queue at a restaurant.
Person 1 → Order → Receive food
↓
Person 2 → Order → Receive food
↓
Person 3 → Order → Receive food
Person 2 has to wait until Person 1's order is completed.
This is similar to synchronous execution.
2. Asynchronous JavaScript
Asynchronous means JavaScript can start a task and continue executing other code without waiting for that task to finish.
When the asynchronous task is completed, its callback can be executed later.
A common example is setTimeout().
console.log("Start");
setTimeout(() => {
console.log("Task completed");
}, 2000);
console.log("End");
Output:
Start
End
Task completed
Task completed appears after 2 seconds.
Why Does This Happen?
When JavaScript reaches setTimeout(), it schedules the function to run after 2 seconds and continues executing the next statement.
So the execution happens approximately like this:
Start
↓
setTimeout() → wait in background
↓
End
↓
After 2 seconds
↓
Task completed
This is called asynchronous execution.
Synchronous vs Asynchronous
| Synchronous | Asynchronous |
|---|---|
| Executes one task at a time | Allows other code to continue while waiting |
| Executes in order | Completion may happen later |
| Next task waits for the current task | Next task does not necessarily wait |
| Can block execution when a task takes time | Helps prevent waiting/blocking |
| Example: normal statements | Example: setTimeout(), API requests |
Important Point
Asynchronous does not mean JavaScript executes everything at exactly the same time.
Instead, it means JavaScript can handle tasks that take time without blocking the execution of other code.
For example, while waiting for a network response, JavaScript can continue handling other tasks.
Common Asynchronous Operations
Some common examples of asynchronous operations in JavaScript are:
setTimeout()setInterval()- API requests
- Fetching data from a server
- Reading files
- Promises
async/await
Conclusion
Synchronous JavaScript executes statements one by one in order, where each task waits for the previous task to finish. Asynchronous JavaScript allows time-consuming tasks to be handled without stopping the execution of other code.
Understanding synchronous and asynchronous execution is important because it helps us understand concepts such as callbacks, setTimeout(), Promises, API calls, and async/await in JavaScript.
Top comments (0)