JS-Dev-101 practice question 24 of 25

Refer to the code below: 01 let sayHello = () => { 02 console.log('Hello, World!'); 03 }; Which code executes sayHello once, two minutes from now?

Full set: 148 questions
Answer options
A: delay(sayHello, 120000);
B: setTimeout(sayHello, 120000); Correct answer
C: setTimeout(sayHello(), 120000);
D: setInterval(sayHello, 120000);
Correct Answer:
B. setTimeout(sayHello, 120000);
Explanation

The browser timer API specifies setTimeout(callback, delay) to invoke the callback once after the delay (in milliseconds). Passing the function reference and 120 000 ms (2 minutes) therefore schedules a single execution of sayHello exactly once after two minutes.

Why Incorrect

A. delay() is not a standard timer API; undefined in browsers.
C. sayHello() immediately executes and the return value (undefined) is given to setTimeout, so nothing runs later.
D. setInterval repeats the call every 120 000 ms instead of running it only once.

References

1. WHATWG HTML Standard, §8.6.1 “Timers”, Definition of setTimeout(callback, delay).

2. Mozilla Developer Network (MDN) – setTimeout, “callback is executed once after delay”.

Scroll to Top