Synchronous :
Synchronous means tasks are executed one after another, in sequence, and each task must complete before the next one starts.It is blocking in nature.
Example :
console.log("Task 1");
console.log("Task 2");
console.log("Task 3");
Output :
Task 1
Task 2
Task 3
//Each line waits for the previous one to finish.
Asynchronous :
Asynchronous means tasks can be started and then handled later, allowing the program to continue executing other tasks in the meantime.It is non-blocking in nature.
Example :
console.log("Task 1");
setTimeout(() => {
console.log("Task 2 (after 2 seconds)");
}, 2000);
console.log("Task 3");
Output :
Task 1
Task 3
Task 2 (after 2 seconds)
//Task 2 is delayed, but the program doesn’t stop—it moves to Task 3.
Top comments (0)