JavaScript is a single-threaded programming language, which means it executes one task at a time on the main thread.

1. What is Synchronous JavaScript?
Synchronous JavaScript executes code one line at a time, in a specific order.

Each statement must finish executing before the next statement starts. This means that if one operation takes a long time, the remaining code has to wait.

This behavior is also known as blocking execution.

Example:

console.log("First"); console.log("Second"); console.log("Third"); 

output :
First
Second
Third

synchronous JavaScript executes one operation at a time.

2. What is Asynchronous JavaScript?
Asynchronous JavaScript allows certain operations to start without making the main JavaScript thread wait for them to finish.

While an asynchronous operation is waiting for a result, JavaScript can continue executing other available code.

This makes applications more responsive and efficient, especially when working with API requests, timers, and file operations.

Example :

setTimeout(() => { console.log("Analysis"); setTimeout(() => { console.log("Design"); setTimeout(() => { console.log("Develop"); setTimeout(() => { console.log("Testing"); setTimeout(() => { console.log("Deployment"); },1000) },2000) },3000) },1000) },2000); console.log("customer calling ...."); 

output :
customer calling...
Analysis
Design
Development
Testing
Deployment

setTimeout() schedules its callback to execute after approximately for given seconds.
JavaScript does not wait for the timer to finish. It continues executing the next statement.

3. Synchronous Vs Asynchronous JavaScript
Synchronous JavaScript :

  • Executes code sequentially.
  • Each operation must finish before the next statement executes.
  • Can block execution when an operation takes a long time.
  • Easier to understand for simple tasks.
  • Example: Normal function calls.

Asynchronous JavaScript

  • Allows certain operations to run without blocking the main thread.
  • Other code can execute while an asynchronous operation is pending.
  • Helps avoid blocking during waiting operations.
  • Useful for operations involving waiting.
  • Example: setTimeout(), Promises, async/await, API calls.

Asynchronous JavaScript Techniques : **
**A. Callbacks

A callback is a function passed as an argument to another function. It can be executed after an operation completes.

function greet(name, callback) { console.log("Hello " + name); callback(); } function message() { console.log("Welcome to JavaScript!"); } greet("John", message); //Hello John  //Welcome to JavaScript 

B. Promises
A Promise represents the eventual success or failure of an asynchronous operation.
A Promise in JavaScript is an object that represents the result of an asynchronous operation.
A Promise is like making a promise to get something done in the future.

A Promise has three states:

Pending: The operation is still in progress.
Fulfilled: The operation completed successfully.
Rejected: The operation failed.

Example :

const myPromise = new Promise((resolve, reject) => { let success = true; if (success) { resolve("Operation successful!"); } else { reject("Operation failed!"); } }); myPromise .then((result) => { console.log(result); //Operation successful! }) .catch((error) => { console.log(error); }); 

C. Async and Await
async and await provide a cleaner and more readable way to work with Promises.

async makes a function return a Promise.
await pauses the execution of the surrounding async function until the Promise settles.

function getData() { return new Promise((resolve) => { setTimeout(() => { resolve("Data received successfully!"); }, 2000); }); } async function fetchData() { console.log("Fetching data..."); const result = await getData(); console.log(result); } fetchData(); console.log("Other task"); 

output:
Fetching data...
Other task
Data received successfully!

await pauses the execution of fetchData() while the Promise is pending, but it does not block the main JavaScript thread. Therefore, "Other task" gets printed before the Promise resolves.

5. Where Do We Use Asynchronous JavaScript?
Asynchronous JavaScript is commonly used in real-world applications.

Some examples :

API Calls: Fetching data from a server without blocking the user interface.
Timers: Executing code after a specified delay using setTimeout().
Event Handling: Responding to user interactions such as clicks and keyboard events.
Database Operations: Waiting for database queries to complete in environments such as Node.js.
File Operations: Reading and writing files asynchronously in Node.js.

6. How Does JavaScript Handle Asynchronous Operations?
JavaScript uses the Event Loop to coordinate asynchronous callbacks and the execution of synchronous code.

Some important components are:

Call Stack: Executes JavaScript functions.
Web APIs: Handle browser-provided operations such as timers and network requests.
Callback Queue (Task Queue): Holds callbacks from tasks such as timers.
Microtask Queue: Holds Promise reactions and other microtasks.
Event Loop: Coordinates when queued callbacks can execute after the call stack becomes empty.
The Event Loop gives microtasks priority over the regular task queue when the call stack is empty.