Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q6 of 15
Node.js - Timers and Scheduling
Identify the error in this code snippet:
const id = setInterval(console.log('Hello'), 1000);
clearInterval(id);
AclearInterval is called before setInterval
Bconsole.log('Hello') is called immediately instead of passing a function
CsetInterval requires a string as first argument
DMissing semicolon after clearInterval
Step-by-Step Solution
Solution:
  1. Step 1: Check setInterval argument

    console.log('Hello') is called immediately, so setInterval receives undefined instead of a function.
  2. Step 2: Effect of immediate call

    This causes 'Hello' to print once immediately, and setInterval does not repeat.
  3. Final Answer:

    console.log('Hello') is called immediately instead of passing a function -> Option B
  4. Quick Check:

    Pass a function, not a function call, to setInterval [OK]
Quick Trick: Pass function reference or arrow function to setInterval [OK]
Common Mistakes:
  • Calling function instead of passing it
  • Ignoring that setInterval needs a function
  • Assuming clearInterval fixes this error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes