Question discussion

What is the output of the following JavaScript code?console.log(1); setTimeout(() => console.log(2), 0); Promise.resolve().then(() => conso…

1365Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the output of the following JavaScript code?

console.log(1);
setTimeout(() => console.log(2), 0);
Promise.resolve().then(() => console.log(3));
console.log(4);
  • A1 2 3 4
  • B1 4 2 3
  • C1 3 4 2
  • D1 4 3 2
Correct answer: D

Explanation

Synchronous code prints 1 and 4 first; the promise callback (a microtask) runs before the setTimeout callback (a macrotask), so 3 precedes 2.

Discussion 0

Nobody has replied yet. Start the discussion!