Bird
0
0

Consider this code:

medium📝 component behavior Q5 of 15
Node.js - Timers and Scheduling
Consider this code:
let count = 0;
const id = setTimeout(() => {
  count++;
  console.log(count);
}, 500);
clearTimeout(id);
setTimeout(() => console.log(count), 1000);
What will be printed?
A0
B1
Cundefined
DError
Step-by-Step Solution
Solution:
  1. Step 1: Understand the first timeout and clearTimeout

    The first timeout increments count and logs it after 500ms, but it is cancelled before running.
  2. Step 2: Analyze the second timeout

    The second timeout logs count after 1000ms; count remains 0 because first timeout never ran.
  3. Final Answer:

    0 -> Option A
  4. Quick Check:

    Cancelled timeout does not change variables [OK]
Quick Trick: Cancelled timeout code does not run; variables unchanged [OK]
Common Mistakes:
  • Assuming count increments despite clearTimeout
  • Expecting error due to variable usage
  • Confusing order of timeouts

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes