DEV Community

Bhuvana Sri R
Bhuvana Sri R

Posted on

Synchronous and Asynchronous in JavaScript

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.
Enter fullscreen mode Exit fullscreen mode

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.

Enter fullscreen mode Exit fullscreen mode

Top comments (0)