Explain the difference between synchronous and asynchronous functions in JavaScript
By FrontendPro Editorial Team Updated 8/8/2026
Answer
Synchronous functions are blocking while asynchronous functions are not. In synchronous functions, statements complete before the next statement is run. As a result, programs containing only synchronous code are evaluated exactly in order of the statements. The execution of the program is paused if one of the statements takes a very long time.
function sum(a, b) {
console.log("Inside sum function");
return a + b;
}
const result = sum(2, 3); // The program waits for sum() to complete before assigning the result
console.log("Result: ", result); // Output: 5
Asynchronous functions usually accept a callback as a parameter and execution continues on to the next line immediately after the asynchronous function is invoked. The callback is only invoked when the asynchronous operation is complete and the call stack is empty. Heavy duty operations such as loading data from a web server or querying a database should be done asynchronously so that the main thread can continue executing other operations instead of blocking until that long operation completes (in the case of browsers, the UI will freeze).
function fetchData(callback) {
setTimeout(() => {
const data = { name: "John", age: 30 };
callback(data); // Calling the callback function with data
}, 2000); // Simulating a 2-second delay
}
console.log("Fetching data...");
fetchData((data) => {
console.log(data); // Output: { name: 'John', age: 30 } (after 2 seconds)
});
console.log("Call made to fetch data"); // This will print before the data is fetched
