Bird
0
0

What will be logged to the console when running this code?

medium📝 Predict Output Q5 of 15
Node.js - Debugging and Profiling
What will be logged to the console when running this code?
let count = 0;
setTimeout(() => {
count += 5;
console.log(count);
}, 0);
console.log(count);
A0 then 5
B5 then 0
C5 then 5
D0 then 0
Step-by-Step Solution
Solution:
  1. Step 1: Understand setTimeout behavior

    setTimeout with 0 delay runs after the current code finishes, so console.log(count) outside runs first.
  2. Step 2: Trace the output order

    First console.log prints 0, then after timeout callback runs, it prints 5.
  3. Final Answer:

    0 then 5 -> Option A
  4. Quick Check:

    Async setTimeout logs = 0 then 5 [OK]
Quick Trick: setTimeout runs after current code, so logs delayed [OK]
Common Mistakes:
  • Assuming setTimeout runs immediately
  • Expecting 5 then 0 output
  • Thinking both logs print same value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes