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:
Step 1: Check function syntax for setTimeout
The first argument must be a function, not the result of a function call.
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.
Final Answer:
setTimeout(() => console.log('Hi'), 0); -> Option D
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
Master "Timers and Scheduling" in Node.js
9 interactive learning modes - each teaches the same concept differently