8.
What will be the output of the following code?
function greetWithDelay(name, delayInMilliseconds) {
setTimeout(function() {
console.log(`Hello ${name}!`);
}, delayInMilliseconds);
}
greetWithDelay("John", 2000);
greetWithDelay("Alice", 1000);
Hello Alice! only
Hello John! only
Hello Alice! followed by Hello John!
Hello John! followed by Hello Alice!
Correct: C
The setTimeout function is used to delay the execution of a function for a specified number of milliseconds. In this code, the greetWithDelay function is called twice, first with the name "John" and a delay of 2000 milliseconds, and then with the name "Alice" and a delay of 1000 milliseconds. Therefore, the Hello Alice! message will be printed after 1000 milliseconds, and the Hello John! message will be printed after 2000 milliseconds.