0

What will be the output of the following JavaScript code and why?

author
kallyani
medium
0
5
console.log("start");

Promise.resolve().then(() => {
console.log("promise");
});

console.log("end");
Answer

The output will be:

start
end
promise


  • JavaScript executes synchronous code first.
  • console.log("start") runs immediately.
  • Promise.resolve().then(...) schedules its callback in the microtask queue.
  • console.log("end") runs immediately after, because it is synchronous.
  • After the call stack is empty, JavaScript executes microtasks.
  • The Promise callback runs and logs "promise".

Click to Reveal Answer

Tap anywhere to see the solution

Revealed

Comments0