DEV Community

Kuganesh S
Kuganesh S

Posted on

Difference Between Synchronous and Asynchronous in JS?

Synchronous:

  • Synchronous is a line-by-line execution process.
  • New operation will not run until the previous one is completed.It is a single threaded programming language.
  • It follows a blocking execution model.
  • The order of execution is easily predictable.
  • Long running tasks stop the entire program.
  • Easy to read and debug due to its sequential order.
  • Browser user interface may freeze or slow during heavy synchronous tasks.
console.log("Morning");
console.log("Afternoon");
console.log("Night");
Enter fullscreen mode Exit fullscreen mode

Asynchronous:

  • Asynchronous able to keep running program without waiting for the current task to finish.
  • It follows a non-blocking execution model.
  • It uses callbacks, Promises, and async/await.
  • It is mainly used in API calls,timers and file reading.
  • Execution order is not always linear.
  • It improves performance and responsiveness.
  • In asynchronous, Promises help to avoid callback hell.
console.log("Morning");

setTimeout(() => {
      console.log("Afternoon");
      }, 1000);

console.log("Night");
Enter fullscreen mode Exit fullscreen mode

Top comments (0)