Bird
0
0

Which of the following is the correct syntax to schedule a function to run after 0 milliseconds in Node.js?

easy📝 Syntax Q12 of 15
Node.js - Timers and Scheduling
Which of the following is the correct syntax to schedule a function to run after 0 milliseconds in Node.js?
AsetTimeout(console.log('Hi'))
BsetTimeout(console.log('Hi'), 0);
CsetTimeout(0, () => console.log('Hi'));
DsetTimeout(() => console.log('Hi'), 0);
Step-by-Step Solution
Solution:
  1. Step 1: Check function syntax for setTimeout

    The first argument must be a function, not the result of a function call.
  2. Step 2: Analyze each option

    setTimeout(() => console.log('Hi'), 0); passes a function that logs 'Hi' after 0 ms delay correctly. setTimeout(console.log('Hi'), 0); calls console.log immediately and passes its result (undefined). setTimeout(0, () => console.log('Hi')); passes 0 (number) as first argument instead of a function, causing a TypeError on execution. setTimeout(console.log('Hi')) calls console.log immediately without delay argument.
  3. Final Answer:

    setTimeout(() => console.log('Hi'), 0); -> Option D
  4. Quick Check:

    Function as first argument = setTimeout(() => console.log('Hi'), 0); [OK]
Quick Trick: Pass a function, not a function call, to setTimeout [OK]
Common Mistakes:
  • Calling the function immediately inside setTimeout
  • Omitting the delay argument
  • Passing non-function as first argument

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes