What is the purpose of the setTimeout() function in JavaScript?

What is the purpose of the setTimeout() function in JavaScript?

a) To delay the execution of a function by a specified amount of time
b) To execute a function repeatedly after a set interval
c) To set a function to run immediately
d) To terminate the execution of a function

Answer:

a) To delay the execution of a function by a specified amount of time

Explanation:

The setTimeout() function allows you to delay the execution of a function for a specified number of milliseconds. It only runs the function once after the delay.

Example:


setTimeout(function() {
    console.log("Hello, after 2 seconds!");
}, 2000);
    

Reference:

JavaScript MCQ (Multiple-Choice Questions)

Scroll to Top