cswork / OAOpen practice workspace

PaycomOA #18

Q18. JS Event Loop — setTimeout Order

Estimated EasyConcept questionStatement and editorial — judging is being prepared

Topics (AI-labelled)Simulation

In what order will the numbers 1-4 be logged to the console when the code below is executed?

(function() {
 console.log(1);
 setTimeout(function(){ console.log(2); }, 1000);
 setTimeout(function(){ console.log(3); }, 0);
 console.log(4);
})();
  • 1 2 3 4 / 4 3 2 1 / 1 4 3 2 <- / 1 3 4 2

Sync log fires first (1, 4); both setTimeout callbacks go to the macrotask queue — the 0ms one fires before the 1000ms one (3, then 2).

Editorial

Loading solution…