Bird
0
0

What will be logged to the console when the following code runs?

medium📝 component behavior Q4 of 15
Node.js - Timers and Scheduling
What will be logged to the console when the following code runs?
const timer = setTimeout(() => console.log('World'), 1500);
clearTimeout(timer);
console.log('Finished');
AFinished
BWorld
CFinished\nWorld
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Analyze setTimeout and clearTimeout

    The setTimeout schedules 'World' to be logged after 1.5 seconds.
  2. Step 2: Effect of clearTimeout

    The clearTimeout(timer) cancels the scheduled callback before it executes.
  3. Step 3: Immediate console.log

    console.log('Finished'); runs immediately and logs 'Finished'.
  4. Final Answer:

    Finished -> Option A
  5. Quick Check:

    Cancelled timeouts do not run [OK]
Quick Trick: clearTimeout prevents callback; immediate logs still run [OK]
Common Mistakes:
  • Assuming the timeout callback still runs after clearTimeout
  • Expecting both 'Finished' and 'World' to print
  • Thinking clearTimeout delays the callback instead of cancelling

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes