DEV Community

S Sarumathi
S Sarumathi

Posted on

Synchronous and Asynchronous

Synchronous:

  • Synchronous means JavaScript runs code one line at a time, in order.

  • A line must finish completely before the next one starts.

  • Think of it like standing in a single queue.

  • The next person can move only after the current person finishes.

Example:

console.log("A");
console.log("B");
console.log("C");
Enter fullscreen mode Exit fullscreen mode

Output:
A
B
C

Asynchronous:

  • Asynchronous means JavaScript can start a task and continue to the next task without waiting for the first one to finish.

  • It allows JS to do multiple things smoothly instead of freezing.

Example:

console.log("A");

setTimeout(() => {
  console.log("B");
}, 2000); // runs after 2 seconds

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

Output:
A
C
B

Top comments (0)