«Back to Home

Learn JavaScript

Topics

Synchronous vs Asynchronous JavaScript

JavaScript is a single-threaded language — it can run only one task at a time.
But modern apps require tasks like:

  • Fetching data from a server

  • Waiting for a timer

  • Reading files

  • Handling user clicks

  • Animations

  • Network calls

If JavaScript handled all tasks synchronously, the browser would freeze and become unresponsive.

That’s why JavaScript uses asynchronous behavior to perform long tasks without blocking the main thread.

This is one of the most important concepts for any JavaScript developer.

What Is Synchronous JavaScript?

Synchronous means one task at a time, in exact order, line-by-line.

If one line takes time, the whole program waits.

Example:

console.log("Task 1");
console.log("Task 2");
console.log("Task 3");

Output:

Task 1Task 2Task 3

Everything runs top to bottom.

Blocking Example (Synchronous)

function wait() {
    let start = Date.now();
    while (Date.now() - start < 3000) {} // 3 seconds freeze
}

console.log("Start");
wait();
console.log("End");

Output (after 3 seconds freeze):

StartEnd

During the freeze, the browser cannot:

  • Scroll

  • Click

  • Type

  • Do anything

This is blocking code and should be avoided.

What Is Asynchronous JavaScript?

Asynchronous means JavaScript can start a task, and continue executing other tasks while waiting for the first to finish.

Examples of async actions:

  • setTimeout

  • setInterval

  • Fetch API / HTTP calls

  • Database operations

  • Event listeners

These tasks are handled using:

  • Callback functions

  • Promises

  • async/await

Example: Asynchronous Behavior With setTimeout

console.log("Start");

setTimeout(() => {
    console.log("Inside Timeout");
}, 2000);

console.log("End");

Output:

StartEnd
Inside Timeout

Even though timeout is 2 seconds, JavaScript does not wait.
It continues executing the next lines.

Why Does setTimeout Run Later?

Because JavaScript uses:

  • Call Stack ? Runs main code

  • Web APIs ? Handles async tasks

  • Callback Queue ? Stores finished async tasks

  • Event Loop ? Moves tasks from queue to stack

This system allows JavaScript to stay responsive.

Real-Life Example: API Call Simulation

console.log("Making API call...");

setTimeout(() => {
    console.log("Data received from server");
}, 3000);

console.log("Other work can continue...");

Output:

Making API call...
Other work can continue...
Data received from server

This is how websites remain fast while loading data.

Synchronous vs Asynchronous (Easy Comparison)

SynchronousAsynchronous
Executes line by lineRuns tasks without waiting
Blocks the codeNon-blocking
Browser can freezeBrowser stays responsive
Used for simple calculationsUsed for timers, API calls, events
One at a timeCan run multiple tasks in background

Example Program (Clear Difference)

Synchronous Example:

console.log("Start");

for (let i = 0; i < 3; i++) {
    console.log("Loop:", i);
}

console.log("End");

Output:

StartLoop: 0Loop: 1Loop: 2End

Asynchronous Example:

console.log("Start");

setTimeout(() => {
    console.log("Timeout finished");
}, 0);

console.log("End");

Output:

StartEnd
Timeout finished

Even with a 0 ms delay, the timeout runs after the main code.

Real-Life Example: Button Click

JavaScript does not wait for user input.

console.log("Waiting for click...");

button.addEventListener("click", () => {
    console.log("Button clicked!");
});

The event listener is asynchronous.

Common Mistakes Beginners Make

  1. Thinking setTimeout(0) runs immediately

  2. Expecting async code to run in order

  3. Mixing async and sync code without understanding behavior

  4. Trying to return values from inside async functions

  5. Not using Promises or async/await correctly

Practice Tasks (Do It Yourself)

  1. Write synchronous code that prints 1 ? 5.

  2. Write asynchronous code with setTimeout that prints a message after 2 seconds.

  3. Simulate an API call with setTimeout.

  4. Create a simple countdown timer.

  5. Write code that prints:

    • "Start"

    • "End"

    • "Data loaded" after 1 second